I am given a string which has uppercase and lowercase letters,spaces,digits and other symbols.A word is any sequence which is delimited by spaces and contains at least a letter.
The words of the string that have the maximum length must be replaced with their reverse(i.e. "apple" becomes "elppa") and the rest of the words must remain unchanged.
Here is my code :
#include <iostream>
#include <string.h>
using namespace std;
char s[255],aux[50][50],*p;
int n,maxx;
bool isword(char s[])
{
int c=0;
for(int i=0;i<strlen(s);i++)
if(s[i]>='A'&&s[i]<='z')c++;
if(c==0)return false;
else return true;
}
void rev(char s[])
{
int n=strlen(s);
for(int i=0;i<n/2;i++)
swap(s[i],s[n-i-1]);
}
int main()
{
cin.get(s,255);cin.get();
p=strtok(s," ");
while(p)
{
n++;
strcpy(aux[n],p);
if(maxx<strlen(aux[n]))maxx=strlen(aux[n]);
p=strtok(NULL," ");
}
for(int i=1;i<=n;i++)
if(strlen(aux[i])==maxx&&isword(aux[i]))
rev(aux[i]);
for(int i=1;i<=n;i++)
cout<<aux[i]<<" ";
}
I get Wrong Answer on two of the tests of this problem(I can't see their input) and I can't figure out what mistake I made.
P.S: Please refrain from commenting on my code being a mix of C and C++.Unfortunately this is the national curriculum here and I have to abide by it.
Comments
Post a Comment