Design a C program to reverse a string but the special characters must be in same location only the character must be reversed.
int CheckIfAlphabet(char c)
{
return ( (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') );
}
void ReverseString(char s[])
{
int k, l = 0;
char temp;
k = strlen(s) - 1;
while (l < k)
{
if (!CheckIfAlphabet(s[l])) l++;
else if(!CheckIfAlphabet(s[k])) k--;
else
{
temp = s[l];
s[l]=s[k];
s[k]=temp;
l++;
k--;
}
}
}
int main()
{
char str[] = "a&b#c$d*e@f";
int n;
printf("\nInpuyt String: ");
for(n=0;n<strlen(str);n++) printf("%c ",str[n]);
ReverseString(str);
printf("\n\nOutput String: ");
for(n=0;n<strlen(str);n++) printf("%c ",str[n]);
return 0;
}
Comments
Leave a comment