Thread: Problem with the palindrome

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    Problem with the palindrome

    hi
    can anyone help me out of this program


    /*program to determine a palindrome*/
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>

    int main()
    {
    int check(char *start);
    char word[40];
    int result;

    printf("\nEnter the string\t");
    scanf("%s",word);

    result=check(word);

    if(result==1)
    printf("\n%s is a palindrome\n",word);
    else
    printf("\n%s is not a palindrome\n",word);


    getche();
    return 0;
    }

    int check(char *start)
    {
    char *end;
    int i,len=0;

    while(*start!='\0')
    {
    len++;
    *start++;
    }
    start=start-len;
    end=start+len;

    for(i=0;i<len/2;i++)
    {
    if(*start==*end)
    {
    *start++;
    *end--;
    return 1;
    }
    else
    return 0;
    }
    }

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    You complicated this program too much.

    Use this...

    int Palindrome(char String[100])
    {
    int counter1=0, counter2=0, counter3=0;

    counter2 = strlen(String);
    for (counter1=0;counter1<=strlen(String); counter1--)
    {
    counter2--;
    if (String[counter1] == String[counter2])
    {
    counter3 = 1;
    }
    }
    return counter3;
    }

    It's easy. The string is read from the beginning to the end and reverse.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Use this as a template for your program. Since this is only one solution, see if you can come up with another.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void){
    	char string[1024];
    	int tag, count, back, flag, loop = 1;
    
    	while(loop == 1){
    		flag = 1;
    
    		puts("Enter a word, phrase, or sentence. Or type 'END' to exit:");
    		for(count = 0; (string[count = getchar()) != '\n'; ++count)
    			;
    		if((toupper(string[0]) == 'E') && (toupper(string[1]) == 'N') &&
    			(toupper(string[2]) == 'D'))
    			break;
    		tag = count - 1;
    
    		for((count = 0, back = tag); count <= tag/2; (++count, --back)){
    			if(string[count] != string[back]){
    				flag = 0;
    				break;
    			}
    		}
    
    		for(count = 0; count <= tag; ++count)
    			putchar(string[count]);
    		if(flag == 1)
    			puts(" is a palindrome\n");
    		else
    			puts(" is not a palindrome\n");
    	}
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    >> if((toupper(string[0]) == 'E') && (toupper(string[1]) == 'N') &&
    (toupper(string[2]) == 'D'))
    <<

    Optimized:
    Code:
    strupr(string);
    
    if (!strcmp("END", string))
    /*** ETC ***/
    1978 Silver Anniversary Corvette

  5. #5
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    strupr(string);

    if (!strcmp("END", string))
    /*** ETC ***/



    Does not seem to work...FYI

    Mattz

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Let me see all of your code. Don't forget to use the code tags to make it readable.
    1978 Silver Anniversary Corvette

  7. #7
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Here is my first attempt. Modified it a bit and it seems to work..but noticed its probably not in the right place as it should print out the contents of buffer when I type QUIT instead of just abruptly ending.
    <code>
    #include <stdio.h>
    #include <string.h>

    int main()
    {

    /* A struct is used to define the character
    array */

    struct str_array{
    char buffer[50];
    };



    int i; /* to be used as an index */

    char in_val[50]; /* used within gets() */

    struct str_array buf[10]; /* an array of
    struct str_array

    buf[0] .. buf[9] */

    /* as a demo, prompts for seven strings are
    provided */



    for (i=0; i<=6; i++)
    {

    printf("Enter a string -> ");
    fflush(stdin); /* definitely, for gets() */
    gets(in_val); /* input string */

    /* syntax of strcpy(a,b) is such that a
    is the "destination" and b is the
    "source" */

    strcpy(buf[i].buffer, in_val); /* note
    buf[i].buffer */

    /* some light testing is performed */

    printf("Just after gets now!\n");
    printf("String provided as input was: ");
    puts(in_val);

    if (strcmp("QUIT", in_val)==0)

    return 0;


    }

    /* contents of the array of structs
    provided as output */

    for (i=0; i<=6; i++)
    {

    puts(buf[i].buffer);

    }

    return 0;

    }
    </code>

  8. #8
    Unregistered
    Guest
    This should help:

    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    char string1[20];
    puts("Enter string one.");
    gets(string1);
    char string2[20];
    string2=strrev(string1);
    if (strcmp(string1,string2)==0)
    {
    puts("Palindrome.");
    }
    else
    {
    puts("Not a palindrome.");
    }
    return 0;
    }
    Just an idea for a syntax. Cya.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char string2[20];
    string2=strrev(string1);
    No. This is incorrect. You cannot use an array to handle a char* return value like this. 'string2' must be a 'char*'.

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

  10. #10
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    QUZAH,
    Any ideas on how to fix this program I posted earlier? Want to have the user type "QUIT" to exit and then print out the string arrays.

    Mattz

  11. #11
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    or if they actually fill the 7 arrays that are counted it prints.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindrome, problem with isalpha
    By Kyeong in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 01:28 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM