Time spent here:

Sunday, 19 June 2016

REVERSE STRING IN SPECIAL ORDER

Problem statement:
Given a string, that contains special character together with alphabets (‘a’ to ‘z’ and ‘A’
to ‘Z’), reverse the string in a way that special characters are not affected.

E.g.:
Input:
str = "a,b$c"

Output:
str = "c,b$a"

Note that $ and, are not moved anywhere.
Only sub-sequence "abc" is reversed

Input:
str = "Ab,c,de!$"

Output:
str = "ed,c,bA!$"

CODE:

#include <stdio.h>
#include<string.h>
int main()
{
    char a[100];
    scanf("%s",a);
    int i=0,j=strlen(a)-1;
    while(1)
        {
        char temp;
        if(i>j)
         { break;}
    if((a[i] >= 65 && a[i]<91  || a[i]>=97 && a[i]<=122) && (a[j] >= 65 && a[j]<91
                    || a[j]>=97 &&a[j]<=122))
          {
              temp=a[i];
              a[i]=a[j];
              a[j]=temp;
              j--;i++;
        }
        if(a[i] < 65 || a[i]>91  && a[i]<97 || a[i]>=122)
           i++;
        if ((a[j] < 65 || a[j]>91  && a[j]<97 || a[j]>=122))
          j--;
     
    
    }
    printf("%s",a);
    return 0;

}


No comments:

Post a Comment