Thread: Palindrome

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Palindrome


    hi can someone help with making a program in C to find the largest palindrom in a sentence?tried this but is not working
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    polidrom (char t[], int left, int right)
    {
    int r;
    if(left>=right) return 0;
    else if(t[left]==t[right])
    polidrom(t,left+1,right-1);
    else return 1;
    }

    void main()
    {
    char sir[256],*cuvint,*separator=" ,.!?;:",pdrommax;
    int rez,m,mmax=0,i;
    puts("Introdu textul:");
    gets(sir);
    for(i=0;i<strlen(sir);i++)
    {

    cuvint=strtok(sir,separator);
    printf("%s\n",cuvint);
    getch();
    while(cuvint)
    {
    m=strlen(cuvint);
    printf("The word <%s> has %d characters\n",cuvint,m);
    getch();
    rez=polidrom(cuvint,0,strlen(cuvint)-1);
    if(rez==0) printf("The word %s is palindrom\n",cuvint);
    else printf("The word is not palindrome \n",cuvint);
    if(cuvint>pdrommax)
    { pdrommax=cuvint;
    if(m>mmax) mmax=m;
    printf("The longest palindrome <%s> has %d characters\n",pdrommax,mmax);
    }
    }

    }
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Originally Posted by devilipod
    hi can someone help with making a program in C to find the largest palindrom in a sentence?tried this but is not working
    What does "but is not working" mean?

    Doesn't compile?
    Compiles but crashes when you run it?
    Ignores all or certain inputs when you run it?
    Accepts input but produces output that is not what you expect?

    What does "but is not working" mean? You need to be much more specific. And you need to supply supporting information too (eg, error and warning messages, what you input and what output you get and what you think it should be).

    Just in case you arrived here with a false notion, I assure you in no uncertain terms that we are not able to read your mind, so you need to tell us the stuff we need to know in order to help you. We are not here to play guessing games with you.

    Use code tags! Otherwise, your code will be unreadable and hence we will not bother to read it. Here is how your program looks with code tags:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    polidrom (char t[], int left, int right)
    {
        int r;
        if(left>=right) return 0;
        else if(t[left]==t[right])
            polidrom(t,left+1,right-1);
        else return 1;
    }
    
    void main()
    {
        char sir[256],*cuvint,*separator=" ,.!?;:",pdrommax;
        int rez,m,mmax=0,i;
        puts("Introdu textul:");
        gets(sir);
        for(i=0;i<strlen(sir);i++)
        {
    
        cuvint=strtok(sir,separator);
        printf("%s\n",cuvint);
        getch();
        while(cuvint)
           {
        m=strlen(cuvint);
        printf("The word <%s> has %d characters\n",cuvint,m);
        getch();
        rez=polidrom(cuvint,0,strlen(cuvint)-1);
        if(rez==0) printf("The word %s is palindrom\n",cuvint);
        else printf("The word is not palindrome \n",cuvint);
        if(cuvint>pdrommax)
               { pdrommax=cuvint;
                 if(m>mmax) mmax=m;
                 printf("The longest palindrome <%s> has %d characters\n",pdrommax,mmax);
               }
           }
    
        }
    }
    When I compile it (MinGW gcc -Wall), this is what I get:
    C:>gcc -Wall polidrom.c
    polidrom.c:6: warning: return-type defaults to `int'
    polidrom.c: In function `polidrom':
    polidrom.c:7: warning: unused variable `r'
    polidrom.c:12: warning: control reaches end of non-void function
    polidrom.c: At top level:
    polidrom.c:15: warning: return type of `main' is not `int'
    polidrom.c: In function `main':
    polidrom.c:33: warning: too many arguments for format
    polidrom.c:34: warning: comparison between pointer and integer
    polidrom.c:35: warning: assignment makes integer from pointer without a cast
    polidrom.c:37: warning: format argument is not a pointer (arg 2)

    C:>
    You need to correct all those warnings before you even think of running your program. Especially the ones on Lines 12, 33, 34, 35, and 37.

    One that it didn't seem to catch is that you are using pdrommax on line 34 before you ever assign a value to it, AKA "using uninitialized variable". Guess there were already too many problems for it to have had time to notice.

    Always have your compiler display all warnings. Read and correct all warnings before you even dream of actually running the program. Warnings are far more important than error messages are. Never ignore warnings.

    And you really need to declare the return type of the polidrom function.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    srr my bad, i mean the program works but the problem is that when i give him the text, he analyse only the first word if its palindrom or not
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    So did you make any corrections to the problems that I've pointed out to you?

    PS

    Code:
    polidrom (char t[], int left, int right)
    {
        int r;
        if(left>=right) return 0;
        else if(t[left]==t[right])
            polidrom(t,left+1,right-1);
        else return 1;
    }
    What does polidrom return? An int, right? So declare it as returning an int!

    And also always return an int! When you return from that recursive call of polidrom in the else if, what do you return? Nothing. That is what that warning, "control reaches end of non-void function", is telling you. You need to take return the value that that recursive call to polidrom just returned to you. Otherwise, you completely defeat the purpose of that function.

    Code:
        for(i=0;i<strlen(sir);i++)
        {
            cuvint=strtok(sir,separator);
            printf("%s\n",cuvint);
            getch();
            while(cuvint)
            {
    Whatever are you trying to accomplish with that for-loop? I can see no purpose for it, nor do I ever see i used even once in that loop.

    Originally Posted by devilipod
    when i give him the text, he analyse only the first word if its palindrom or not
    Well, with
    cuvint=strtok(sir,separator);
    you're asking for the first part of that input before you encounter a separator, which you define space to be. It's only doing what you told it to do there.

    But then, each and every time you would reenter that loop, you start all over again from the beginning of sir. That does not make any sense at all.

    Code:
            while(cuvint)
            {
    cuvint has been set to point to the first token in sir, so it is not NULL. Then within the while loop you never change the value of cuvint, so you're trapped inside an infinite loop. You didn't tell me about being trapped inside an infinite loop, why not? I'm not a mind-reader! You need to be forthcoming with pertinent information!

    Code:
                if(cuvint>pdrommax)
                { 
                    pdrommax=cuvint;
    As the warning, "comparison between pointer and integer", tells you, you are trying to compare a pointer (cuvint) to a char (pdrommax; char is an integer type). That is not allowed! Oh, the compiler generated code that will try to do something with that, but who knows what that will do? Certainly it won't do what you expect it to do. That's the danger of running a messed-up program that compiled with warnings: unexpected behavior and unexpected results.

    Similarly, in the next line you are trying to assign a pointer to a char, which is also not allowed. The warning, "assignment makes integer from pointer without a cast", informed you of that problem. Have you corrected it yet? If not, then why not?

    printf("The longest palindrome <%s> has %d characters\n",pdrommax,mmax);
    pdrommax is a char, but "%s" tells printf to expect a pointer, just as the warning, "format argument is not a pointer (arg 2)", had told you. It's going to take the value in pdrommax and try to use it as a pointer, which is a recipe for disaster. It might just print out gibberish as it misinterprets the values at the random memory location that it thinks pdrommax is pointing to. Or it might just crash the program as you try to access memory that doesn't belong to you. Even worse, because we don't know what the program does with that disallowed assigning of a pointer value to a char, the situation here is not unlike playing Russian Roulette.

    Correct your code. Recompile it with warnings turned on and turned up to display all warnings. Do not attempt to run the program until it compiles cleanly, which means without errors and without any warnings. Only then can you begin to debug your program's logic.

    PPS
    Do you know how to use strtok? Have you read the documentation for strtok? If you haven't, then why not?

    In UNIX, C library documentation (AKA "the manual") is traditionally provided on the system through "man pages" (read Wikipedia at http://en.wikipedia.org/wiki/Man_page). Thousands of copies of the man pages have been posted on the Internet and can be found in Google with the search string, man page function_name. Use that resource!

    Google'ing on man page strtok, we get "About 22,900 results". The first result, http://linux.die.net/man/3/strtok, tells us:
    Description

    The strtok() function parses a string into a sequence of tokens. On the first call to strtok() the string to be parsed should be specified in str. In each subsequent call that should parse the same string, str should be NULL.

    The delim argument specifies a set of bytes that delimit the tokens in the parsed string. The caller may specify different strings in delim in successive calls that parse the same string.

    Each call to strtok() returns a pointer to a null-terminated string containing the next token. This string does not include the delimiting byte. If no more tokens are found, strtok() returns NULL.

    A sequence of two or more contiguous delimiter bytes in the parsed string is considered to be a single delimiter. Delimiter bytes at the start or end of the string are ignored. Put another way: the tokens returned by strtok() are always nonempty strings.
    You should have known that before attempting to use strtok. There is absolutely no excuse to not RTFM! ("Read The Manual!").
    Last edited by dwise1_aol; April 17th, 2013 at 01:34 PM.

IMN logo majestic logo threadwatch logo seochat tools logo