Help with strings

November 18, 2009 at 22:12:54
Specs: Windows 7
I have no ideal on how to complete this palindromes checker program, please help me fill out the blank spots, thanks. I tried to fill out some already, but I got stuck on most of them


#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{char str[80];
int strlength;
int j=0;
int front=0,back; int flag=1;
gets(str);
//Get the length of string str
strlength = strlen(str)
//Convert all the characters in the string str to uppercase
for(int i = 0; i < str; i++)
if(str[i])
str[i] = toupper(str[i]);
//Remove all the special characters except letters a - z
for(j = 0; j < str.length; j++)
if(isalpha(str[i]))
{str[j] = str[i];
------------------}
str[j]= '\0';
//front index is pointed to the first character in the string str
front = 0;
//back index points to the last character in the string str
back = ------------------;
//Compare the first character with the last character. If they're are the same,
//compare the next character.
for(i=0; i< j/2 ;i++)
{if(str[------] != str[---------])
{ flag = 0;
break;
}
front----;
back=back -1;
}
if(flag ---------- 0)
cout<<"It is not a palindrome"<<endl;
else
cout<<"It's a palindrome"<<endl;
return 0;
}


See More: Help with strings

Report •


#1
November 19, 2009 at 11:26:27
If you're just dealing with words, then perhaps something like below would do.

int isWordPalindrome(char *src) {

   int i=0, j = strlen(src) - 1, flag = 0;

   while(j >= i) {
      if(toupper(src[i++]) != toupper(src[j--])) { flag = 1; }
   }

   return (flag);         
}


char word[BUFSIZ] = { 0 }, *p;
fgets(word, sizeof word, stdin);

if((p = strchr(word, '\n')) != NULL) {
*p = '\0';
}

printf("%s %s a palindrome", word,
isWordPalindrome(word) == 0 ? "is" : "is not");

HTH


Report •
Related Solutions


Ask Question