Thread: Palindrome

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    Palindrome

    Created a palindrome but it is supposed to ignore the whitespaces in the string. can anyone tell me what's wrong here?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    main(){
    	char mes1[80],mes2[80];
    	printf("Enter a string here --> ");
    /*	scanf("%s",mes1);	*/
    	getline(mes1);
    	strcpy(mes2,mes1);
    	reversi(mes1,mes2);
    	printf("%s  %s\n",mes1,mes2);	
    	if (strcmp(mes1,mes2) == 0)
    		printf(" You entered a palindrome word \n");
    	else
    		printf(" No, the word is NOT a palindrome \n");
    }	
    
    
    getline(m1in)
    char m1in[80];
    {
    	int i=0;
    	char c;
    	while(i<81 && (c=getchar()) !='\n'){
    		m1in[i]=c;
    		++i;
    	}
    	m1in[i]='\0';
    	return;
    }	
    
    reversi(m1,m2)
    char m1[80],m2[80];
    {	
    	int i=0,jl;
    	jl=strlen(m1);
    	while (i<jl){
    		m2[i]=m1[jl-i-1];
    		i++;
    	}
    	return;
    }

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    The second time I'm saying this to you:

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Created a palindrome but it is supposed to ignore the whitespaces in the string.

    How is it supposed to be ignoring whitespace? You are reading space characters in with everything else.
    Code:
    while(i<81 && (c=getchar()) !='\n')
       if(!isspace(c)) /* be selective */
          m1in[i++]=c;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    4

    Sorry

    Sorry, I am new.

    Won't happen again.

  5. #5
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    No problem...Trust me, if I was actually upset you'd know it . Just a pet peeve of the board. Figure out your problem?
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    If you type spaces in for the string, and you want to ignore them in the test, you must remove them before processing.

    If you want to display the original text typed in, you will need a third array "mes3" to hold the string that does not include spaces (commas, periods, and other punctuation?) and have your code operate on mes3 instead. After the test, you can still output mes1.

    Walt

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by WaltP
    If you type spaces in for the string, and you want to ignore them in the test, you must remove them before processing.
    No you don't. Do the following:
    Code:
    ptr1 = start of list;
    ptr2 = end of list;
    
    while( ptr1 )
    {
        while( isspace( ptr1 ) )
            ptr1++;
        while( isspace( ptr2 ) );
            ptr2--;
    
        if( ptr1 != ptr 2 )
            not_a_match( );
    
        increment ptr1
        decrement ptr2
    }
    See? You don't even need an array at all. Just two pointers. You'll need slightly more error checking than that, but that's the basic idea.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    True Q, but I was allowing for the strcmp() as designed as opposed to writing another compare function. This requires less 'error checking', too.

    W

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Recursive String Palindrome Code
    By clegs in forum C Programming
    Replies: 13
    Last Post: 12-21-2008, 12:36 PM
  2. Is it a Palindrome?
    By xp5 in forum C Programming
    Replies: 3
    Last Post: 09-06-2007, 05:26 AM
  3. bool palindrome definition
    By justinc911 in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2003, 05:50 PM
  4. Palindrome Coding trouble
    By TheLoneWolf32 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 07:05 PM
  5. palindrome HELP !!!
    By TED22_8 in forum C Programming
    Replies: 23
    Last Post: 01-22-2002, 02:14 PM