Jump to content

C loop, giving me trouble !! Can anyone help..?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
rossen

rossen

    Newbie

  • Members
  • Pip
  • 4 posts
i have this code to test for palindromes, it functions perfectly well on the first test, but as the program continues to check for more after the first word it keeps printing not palindrome, once it wasnt the first word typed, run it pls, and you'll c what im talking about. Can anyone help me fix it..?

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int isPalindrome( char *s);
int main (void)
{
int i=0;
int ch;
char s[100];
while (1){
while ((ch = getchar()) != ('\n')){
if (isalpha(ch)){
ch=toupper(ch);
s[i]=ch;
i++;
}
}
s[i]='\0';
if (isPalindrome(s) ==1){
printf("is palindrome\n");
}
else{
printf("not palindrome\n");
}
system("Pause");
return 0;
}

thanks,
-------------------------------------------

Edited by v0id, 11 June 2008 - 05:13 AM.


#2
Chinmoy

Chinmoy

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 392 posts
Where is ispalindrome() defined?

God is real... unless declared an integer

my blog :: http://techarraz.com/


#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
In your loop, you are continually incrementing i, so s will become something like the following:
s = "something\0else\0than\0expected\0"

You can see where you'll get issues.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
revealer

revealer

    Newbie

  • Members
  • Pip
  • 9 posts
I can't see isPalindrome() nor isAlpha() anywhere.

#5
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Presumably the functions are defined elsewhere, as rossen managed to get the code to work.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums