-
help on loop
any one know who to make loop on this??
how to loop over and over again till i prest "-1" then only the program end
Code:
#include<stdio.h>
#include<string.h>
void main()
{
char strsrc[5];
char strtmp[5];
printf("\n Enter a five-digit number ( -1 to end ): "); gets(strsrc);
strcpy(strtmp,strsrc);
strrev(strtmp);
if(strcmp(strsrc,strtmp)==0)
printf("\n %s is a palindrome\n",strsrc);
else
printf("\n %s is not a palindrome\n",strsrc);
}
-
-
-
where do i need to put the while?
Mats you mean <math.h>
?
-
Not forgetting the FAQ on void main as well.
-
Code:
#include<stdio.h>
#include<string.h>
void main()
{
char strsrc[5];
char strtmp[5];
printf("\n Enter a five-digit number ( -1 to end ): "); gets(strsrc);
strcpy(strtmp,strsrc);
strrev(strtmp);
while (strsrc!=-1)
if(strcmp(strsrc,strtmp)==0)
printf("\n %s is a palindrome\n",strsrc);
else
printf("\n %s is not a palindrome\n",strsrc);
}
this not working cuz its says !=' : no conversion from 'const int' to 'char *'
-
That's because you don't compare strings with !=. You compare strings with strcmp.
Also, you would need to compare with the string "-1", not the number -1.
-
then its must be like this?
Code:
#include<stdio.h>
#include<string.h>
void main()
{
char strsrc[5];
char strtmp[5];
printf("\n Enter a five-digit number ( -1 to end ): "); gets(strsrc);
strcpy(strtmp,strsrc);
strrev(strtmp);
while (strcmp!="-1")
if(strcmp(strsrc,strtmp)==0)
printf("\n %s is a palindrome\n",strsrc);
else
printf("\n %s is not a palindrome\n",strsrc);
}
this still erroe thou...
-
So if "you" did this one right:
Code:
if(strcmp(strsrc,strtmp)==0)
why do you think it should be
Code:
while (strcmp!="-1")
?
-
because matsp says use while, and i dono where to used the while...
-
Who's complaining about the while? It's the only thing right about that second quoted line.
-
i still dont get you sir.. do you mind correct that code please..
-
Code:
while (strcmp(strcmp, "-1") !=0) {
...
}
You cannot use operators like !=, == with strings. Besides, strcmp is not even a string, it is a pointer to an array of char.
Why test with -1 for a string by the way?? I don't know if this will work. If you enter -1 that will be two characters saved inside strsrc. If you reverse them, with strrev(), I assume you will have 1- as a string, not -1. You can just press "quit" or 'q' to quit the program...
-
StormRoBoT, try doing these tasks first:
- Write a program that reads in a word of no more than 5 letters and prints the word in reverse.
- Write a program that reads in a word and of no more than 5 letters and prints "Correct!" if the word is "Storm" and "Wrong!" otherwise.
- Write a program that reads in words of no more than 5 letters. While the word is not "-1", print the word and read another word, otherwise end the program.
Once you are done with these three tasks, you will have a better idea of how to combine them to solve your current problem.
-
its not working,
any way you got any idea on how to do this in other way? where if i keyin -1 it will terminate the program..
something like this
Code:
Enter a five-digit number ( -1 to end ): 21212
21212is a palindrome
Enter a five-digit number ( -1 to end ): 16633
16633 is not a palindrome
Enter a five-digit number ( -1 to end ): -1