|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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..?
Code:
#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;
}
------------------------------------------- Last edited by v0id; 06-11-2008 at 08:13 AM. |
| Sponsored Links |
|
|
|
|||||
|
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.
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||||
|
Presumably the functions are defined elsewhere, as rossen managed to get the code to work.
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH] "Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly." To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| loop problem | suicidal pencil | General Programming | 1 | 05-13-2008 07:32 PM |
| Javascript: Loop through ids | dirkfirst | JavaScript and CSS | 1 | 07-02-2007 02:28 PM |
| Sounds and Sound Loop Resources | TVDinner | Website Design | 0 | 03-10-2007 08:20 AM |
| Will loop ever terminate??? | kphilux | General Programming | 8 | 03-07-2007 09:55 AM |
| Javaa:Tutorial - The Loop | John | Java Tutorials | 0 | 12-09-2006 10:01 AM |
| Xav | ........ | 1318.93 |
| MeTh0Dz|Reb0rn | ........ | 1053.7 |
| morefood2001 | ........ | 879.43 |
| John | ........ | 877.37 |
| marwex89 | ........ | 869.98 |
| WingedPanther | ........ | 814.88 |
| Brandon W | ........ | 712.1 |
| chili5 | ........ | 300.72 |
| Steve.L | ........ | 233.06 |
| dcs | ........ | 195.72 |
Goal: 100,000 Posts
Complete: 82%