I'm new in C programing language . I have a question how can I end loop in windows.
#include<stdio.h>
#include<conio.h>
int main() {
printf("enter ur palindrome");
int i[100], c, d = 0, n = 0;
c = getchar();
while (c != '\n') {
i[d] = c;
d++;
}
loop:
while (d != 0) {
if ((i[0 + n] != i[d - n])) {
n++;
goto loop;
}
printf("this is not a palindrome");
break;
}
printf("this is a palindrome");
return (0);
}
I HAVE TRIED ALMOST EVERYTHING CTRL+Z, CTRL+C, CTRL+D, REPLACING '\\n' WITH EOF and many more thing. Nothing worked for me. I'm using CodeBlocks on Windows 10. Is there some other way of writing such type of program other then getchar and eof.
you probably want to take a look at this section again
c=getchar();
while(c!= '\n') // Value of c never altered, hence it'll never break
{ i[d]=c;
d++;
}
and yes, the other loop
loop: // not required
while ( d!=0 ) // can't see change in d within this body, is it n ?
{
if((i[0+n] != i[d-n]))
{
n++;
goto loop; // not required
continue; //will do
}
printf("this is not a palindrome");
break;
}
and you'd actually get an extra message saying
this is a palindrome
after printing
this is a not palindrome
which I suppose is not what you want.
In that loop you need to again read next character so need to add getchar()
in that loop
c=getchar();
while(c!= '\n')
{
i[d]=c;
d++;
c=getchar();
}
Here's a different way to write that piece of code
#include <stdio.h>
#include <string.h>
int main()
{
char *word;
int i;
int n;
printf( "enter ur palindrome" );
scanf("%[^\n]", word); // reads input all the way to the new line
n = strlen( word );
for ( i = 0; i < n; i++ ) {
if ( word[ i ] != word[ n-1 - i ] ) {
break; /* for */
}
}
if ( i == n ) {
printf( "This is a palindrome");
} else {
printf( "This is not a palindrome" );
}
return 0;
}
Rather than using goto
, use continue;
to repeat the loop.
However, there are a number of other problems with the posted code.
The array `int i[100]` is never terminated with a NUL byte ('\0')
An array of char not int should be used.
this loop: `while (d != 0) will never exit,
because (if the loop is ever entered)
the `d` variable is never changed within the loop
Here is my suggested code:
caveat: not thoroughly tested
#include <stdio.h>
//#include<conio.h> <-- not used, so do not include
#include <string.h>
#define MAX_LENGTH (100)
int main( void )
{
int d;
int n;
char i[MAX_LENGTH];
printf("enter ur palindrome\n"); // <-- \n so will immediately print
if ( NULL != fgets( i, MAX_LENGTH, stdin ) )
{
if( strlen( "\n" ) < strlen( i ) )
{ // then, some string entered
// remove any trailing '\n'
char *newline = strstr( i, "\n" );
if( newline )
{ // then '\n' found
*newline = '\0';
} // end if
d = strlen( i );
for( n=0; (d-n) >= n; n++ )
{
if( i[0 + n] != i[d - n] )
{ // then no match
printf("this is not a palindrome");
break;
} // end if
} // end for
if( (d-n) < n )
{ // then palindrome
printf("this is a palindrome");
} // end if
}
else
{
printf( "nothing entered\n");
} // end if
} // end if
return (0);
} // end function: main
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.