Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

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

loop

  • Please log in to reply
4 replies to this topic

#1 rossen

rossen

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 11 June 2008 - 04:03 AM

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.

  • 0

#2 Chinmoy

Chinmoy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 365 posts

Posted 11 June 2008 - 04:34 AM

Where is ispalindrome() defined?
  • 0

God is real... unless declared an integer

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


#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 11 June 2008 - 08:08 AM

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.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 revealer

revealer

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 11 June 2008 - 09:28 AM

I can't see isPalindrome() nor isAlpha() anywhere.
  • 0

#5 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 11 June 2008 - 10:45 AM

Presumably the functions are defined elsewhere, as rossen managed to get the code to work.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download