Palindromic String

This is a discussion on Palindromic String within the C Programming forums, part of the General Programming Boards category; Hi i am writing a program to check if a string is palindromic or not. I have looked through the ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Palindromic String

    Hi i am writing a program to check if a string is palindromic or not. I have looked through the forums but nothing really answers my question fully. My code is pretty close i think but i havent used the strcmp function before so it might be the cause!any help would be great

    Code:
    #include <stdio.h>
    
    int main ()
    
    
    {
    
    
    
    
    char string[20],reverse[20];
    
    
    int k,i,j;
    
    
    printf("Please enter a string: ");
    
    
    gets(string);
    i=0;
    
    
    	while (string[i] !='\0')
    	{
    		i++;
    	}
    
    
    i=0;
    
    
    	while (string[i]!='\0')
    	{
    		i++;
    		
    		scanf("%s",&string[i]);
    	}
    	
    	
    j=i;
    
    
    	while (reverse[j]!=0)
    	{
    		
    		scanf("%s",&reverse[j]);
    		
    		j=j-1;
    		
    	}	
    
    
    	while(i!='\0')
    	
    	if (strcmp (string,reverse)==0)
    	{
    		printf("yes");
    	}
    		
    	else 
    	{
    	printf("nt");
    	}
    
    
    return 0;
    
    
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You did not actually describe your problem, you just postulated a cause for it. People (including me) will often skip over posts like that, because if you cannot take the time to express yourself clearly and effectively, why would I?

    However, you are obviously very very very new to programming, so bewilderment and naivete are par for the course. Fair enough, but, for your own good and everyone else's, keep in mind what I just mentioned.

    LOTS of problems here. First, you should #include <string.h> for strcmp(); because strcmp() returns an int (the default prototype), it will work anyway, but that is not a good habit to get into.

    "gets()" is a very depreciated function because it is a security nightmare. Use fgets() instead:

    Code:
    fgets(string, 20, stdin);
    Next, what is the point of this:

    Code:
    i=0;
     
     
        while (string[i] !='\0')
        {
            i++;
        }
     
     
    i=0;
    You set i to 0, i then potentially increments to some unknown value. Then you set i to zero. o_O

    Next this:

    Code:
        while (string[i]!='\0')
        {
            i++;
             
            scanf("%s",&string[i]);
        }
         
         
    j=i;
    The program hangs here because of the scanf() call, which reads from standard input (ie, the user console), but you did not prompt the user to input anything. Also, I am pretty sure you do not actually want the user to input anything beyond the first string. What are you trying to do in this loop?

    It looks to me like you are trying to copy string backward into reverse, which makes sense -- then you can strcmp() for a palindrome. But the way you are trying to do it does not make any sense.

    How about something like:

    Code:
    	int len = strlen(string);
    	for (i = 0; i < len; i++) {\
    		reverse[i] = string[len-1-i];
    	}
    	reverse[i] = '\0';
    There is a small problem with that too, because you will have a newline at the end of the entered string which will mess up the possibility of a palindrome. But hopefully you see the idea...and can ask some specific follow-up questions.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Hi thanks for your reply. Yes i am new to programming apologies for not describing in detail what my problem was. Like you said im not really sure what the problem is!

    Code:
    1 2 3 4 5 6 7 8 9 10
    i=0; while (string[i] !='\0') { i++; } i=0;
    This code was to test the length of the string. My idea was to then reverse the loop starting at this point. I hope this makes sense. However after reading your post it seems obsolete.

    Code:
    while (string[i]!='\0')
    { i++; scanf("%s",&string[i]); }
    j=i;
    This section like you said was to reverse the loop however like you said it stalls the program. Being honest i dont 100% understand what happens in your suggested code..We have yet to cover for loops and although i have an idea how they work im still not positive. I hope this explains the situation further

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindromic numbers problem
    By deadrabbit in forum C Programming
    Replies: 1
    Last Post: 09-27-2011, 09:11 PM
  2. Replies: 22
    Last Post: 07-28-2011, 02:26 PM
  3. Undefined Behaviour (Palindromic Number Finder)
    By pobri19 in forum C++ Programming
    Replies: 12
    Last Post: 09-28-2008, 05:54 AM
  4. checking if binary numbers are palindromic
    By Beatz in forum C Programming
    Replies: 3
    Last Post: 01-24-2008, 01:49 PM
  5. Replies: 1
    Last Post: 10-31-2005, 11:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21