-2

i have tried to make a program to check the pallindrome manually on paper it should work but it doesnt. i have attached the output with the code


#include <stdio.h>      
#include <string.h>      
#include <conio.h>
#include <stdlib.h>

void main()
{
 char str[100];
 int i,l,k;

 printf("type the desired to string to check pallindrome\n");
 gets(str);
 l=strlen(str);
 printf("%d\n", l);

 k=l-1;
 char temp[100];

 for(i=0;i<l;i++)
 {
   temp[k]=str[i];
   --k;

 }

 if(strcmp(temp, str)==0)
 {
   printf("pallindrome\n");
 }
 else
 {
   printf("not a pallindrome\n");
 }


}

here is the output

[1]: https://i.stack.imgur.com/dERFQ.png

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
DevZer0
  • 18
  • 3

2 Answers2

1

You need to append the NUL terminator at the end of temp right after the for loop:

 for(i=0;i<l;i++)
 {
   temp[k]=str[i];
   --k;

 }
 temp[i] = '\0';

Otherwise, the strcmp function will cause UB.

babon
  • 3,615
  • 2
  • 20
  • 20
  • what is a null terminator? and why would strcmp cause a ub without it? I am fairly new to programming so i dont know much......... – DevZer0 Jun 12 '21 at 15:28
  • @DevZer0 `NUL` terminator in C, is the `\0` character appended after a C-string: https://en.wikipedia.org/wiki/Null-terminated_string . `strcmp` will not know how far to read in characters if it is missing and might tread into memory you don't own: https://stackoverflow.com/questions/24353504/whats-wrong-with-strcmp – babon Jun 12 '21 at 17:46
1

For starters according to the C Standard the function main without parameters shall be declared like

int main( void )

The array temp does not contain a string because you forgot to append it with the terminating zero character '\0'.

So the call of the strcmp

if(strcmp(temp, str)==0)

results in undefined behavior.

Also the function gets is unsafe and is not supported by the C Standard. Instead use the function fgets.

Also to check whether a string is a palindrome there is no need to declare an auxiliary array.

The code can look like

printf("type the desired to string to check pallindrome\n");
fgets(str, sizeof( str ), stdin );

str[strcspn( str, "\n" )] = '\0'; // to remove the new line character '\n'

size_t n = strlen( str );
printf( "%zu\n", n );

size_t i = 0;

while ( i < n / 2 && str[i] == str[n-i-1] ) ++i;

if( i == n / 2 )
{
    printf("pallindrome\n");
}
else
{
    printf("not a pallindrome\n");
}

You could write a separate function that checks whether a string is a palindrome.

Here you are.

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

int is_palindrome( const char *s )
{
    size_t n = strlen( s );
    
    size_t i = 0;
    
    while ( i < n / 2 && s[i] == s[n-i-1] ) ++i;
    
    return i == n / 2;
}

int main(void) 
{
    enum { N = 100 };
    char s[N];
    
    printf( "Type a desired string to check whether it is a palindrome: " );
    fgets( s, sizeof( s ), stdin );
    
    s[ strcspn( s, "\n" ) ] = '\0';
    
    if ( is_palindrome( s ) )
    {
        printf( "\"%s\" is a palindrome.\n", s );
    }
    else
    {
        printf( "\"%s\" is not a palindrome.\n", s );
    }
    
    return 0;
}

The program output might look like

Type a desired string to check whether it is a palindrome: abcdedcba
"abcdedcba" is a palindrome
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335