Reverse the string without changing index of space
Question:
Reverse the string without changing the position of space in the sentence and make sure that the letter comes in the place of the capital letter is also capital.
Logic:
- Get the input string from the user.
- Reverse the given string only the alphabets are stored into another array.
- Then copy the reversed elements into a given array only the alphabets.
- The spaces, Uppercase, Lowercase, and punctuation indexes remain unchanged.
- And print the reversed string.
Algorithm:
- Get the string from the user contains spaces, Uppercase, Lowercase, and also punctuations.
- Only the alphabets in the given string are reversed and stored into another array.
- Replace the reversed alphabets with the given string(only alphabets.
- Then the index of spaces, Uppercase, Lowercase, and punctuations are not changed.
- Then print the reversed string stored in the given string space.
Program:
#include<stdio.h> void main() { char a[30],b[30]; int n,m=0,i,j=0; clrscr(); printf("enter the string:"); gets(a); n=strlen(a); i=n-1; while(i>=0) //store the string in another in reverse order contains only alphabets { if(a[i]>='a'&&a[i]<='z') //checks for alphabets and stores into new array { b[m]=a[i]; m++; } else if(a[i]>='A'&&a[i]<='Z') //checks for upper case also stores into array { b[m]=a[i]; m++; } i--; } for(i=0;i<n;i++) { if(a[i]>='a'&&a[i]<='z') //checks for alphabet in lower case { if(b[j]>='a'&&b[j]<='z') { a[i]=b[j]; } else //convert the upper case into lower case { a[i]=b[j]+32; } j++; //increments the index of reversed array } else if(a[i]>='A'&&a[i]<='Z') //checks for alphabet in upper case { if(b[j]>='A'&&b[j]<='Z') { a[i]=b[j]; } else //convert the lower case into upper case { a[i]=b[j]-32; }j++; } } for(i=0;i<n;i++) //prints the reversed string { printf("%c",a[i]); } getch(); }