Consider an input string and reverse the alphabets in the string except the special characters in the string and print the string.
For example:
Consider the input string, s@t#!sh
The expected output is , h@s#!ts
As you can see, the special characters are in place without any change, whereas the alphabets in the string are reversed.
Input:
s@t#!sh
Output:
h@s#!ts
C Implementation:
Solution 1:
For example:
Consider the input string, s@t#!sh
The expected output is , h@s#!ts
As you can see, the special characters are in place without any change, whereas the alphabets in the string are reversed.
Input:
s@t#!sh
Output:
h@s#!ts
C Implementation:
Solution 1:
#include <stdio.h> #include <string.h> int main(void) { // to reverse a string char str[10000]; char temp; int i = 0; int j; scanf("%s",str); // str = "sathish" j = strlen(str)-1; // h s i h t a s \0 // 0 1 2 3 4 5 6 7 while(i<j) { while(i<j && !(str[i]>='a' && str[i]<='z')) i++; while(i<j && !(str[j]>='a' && str[j]<='z')) j--; temp = str[i]; str[i] = str[j]; str[j] = temp; i++,j--; } printf("%s",str); return 0; }
Solution 2:
#include <stdio.h> #include <string.h> int main() { char str[10000]; int i,j; char temp; i = 0; scanf("%s",str); j = strlen(str)-1; while(i<j) { if((str[i]>='a' && str[i]<='z') && (str[j]>='a' && str[j]<='z')) { temp = str[i]; str[i] = str[j]; str[j] = temp; i++,j--; } else { if(str[i]>='a' && str[i]<='z') j--; else i++; } } printf("%s",str); return 0; }