Reverse the string without changing index of space

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:

  1. Get the string from the user contains spaces, Uppercase, Lowercase, and also punctuations.
  2. Only the alphabets in the given string are reversed and stored into another array.
  3. Replace the reversed alphabets with the given string(only alphabets.
  4. Then the index of spaces, Uppercase, Lowercase, and punctuations are not changed.
  5. 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();
}

You might also like:

Reverse the string without changing the punctuation

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.
1 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x