Program Discussion :: Strings
340 / 60
Write a program to reverse an array without affecting special characters.
Note: 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.
Answer:
#include
#include
int isSpecial(char);
int main()
{
char hel[] = "a!!!b.c.d,e'f,ghi";
char *anj = hel;
reverseWord(hel);
printf("%s",anj);
return 0;
}
void reverseWord(char *anj)
{
int r = strlen(anj)-1;
char x;
int l = 0;
while (l='A' && x='a' && x
Asked In ::
Language:
Ashwani
31 Mar, 2018 9:18 PM
#include <stdio.h>
#include <string.h>
int isSpecial(char);
int main()
{
char hel[] = "a!!!b.c.d,e'f,ghi";
char *anj = hel;
reverseWord(hel);
printf("%s",anj);
return 0;
}
void reverseWord(char *anj)
{
int r = strlen(anj)-1;
char x;
int l = 0;
while (l<r)
{
if (!isSpecial(anj[l]))
{
l++; }
else if (!isSpecial(anj[r]))
{
r--;
}
else
{
x = anj[l];
anj[l] = anj[r];
anj[r] = x;
l++;r--;
}
}
}
int isSpecial (char x)
{
return ((x>='A' && x<='Z') || (x>='a' && x<='z'));
}