Thread: Storing in a file?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    Function that "matches"

    I'm trying to make a program where the user enters in a sentence, then the program will tell whether this sentence is a palindrome (sentence that are the same if read forward or backwards) or not.

    palindrome example: Straw Warts

    Is there a function I can use that will compare each character forward with each character backwards?
    Last edited by 98dodgeneondohc; 04-14-2005 at 07:25 AM.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You can think of C-strings as characters arrays and thus you can traverse them letter by letter forward or backward.

    Code:
    for(int i=0; i<n; ++i)
      printf("%c\n", str[i]);
    for(int i=n-1; i >= 0; --i)
      printf("%c\n", str[i]);
    As for comparing characters, that can be done using the == operator. For comparing strings (in normal order), use strcmp().

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    the following program give you an idea of how to check the pallandrom.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
            char str[20];
            int i,j,k;
           fgets(str, sizeof str, stdin);
           
           for(i=0;str[i]!='\0';i++);
           
           for(j=i-2,k=0;j>=0;k++,j--)
           {
           if(str[k]!=str[j])
           {
               printf("Not a Pallandrom");
               getchar();
               return 0;
           }
       }
       printf("Yes! it is a pallandrom");
           getchar();
      }
    hope this helps

    s.s.harish

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Quote Originally Posted by ssharish
    hope this helps
    Do you really think it helps to write someone's program for them?

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    guys, how about if the user enters a sentence that is seperated by a spacebar? What function can I use to remove all spaces from the sentence the user enters, so matching will be easier?

    Another thing, how would I use tolower() with the fgets() function so I can convert every character in the sentence to lowercase?

    How do I enter a null string? ex. (while sentence != --> null <--);




    I appreciate all the help. Thanks everyone!
    Last edited by 98dodgeneondohc; 04-14-2005 at 08:26 AM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    heyyyy, no need of eliminating the spaces check out my program it works fine

    try inputing something like this

    -> hah hah
    check what the output u get

    and the prog what i have posted is just an idea of how to do it. try writing you own program. there are many other ways to do it. and show us your best attempt

    s.s.harish

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    what does the \0 mean in this code harish?

    for(i=0;str[i]!='\0';i++);

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    it is termination of a string called NULL which is placed at the end of the string
    Code:
    str
    [h] [e] [l] [l] [o] [\0]
                               |
                           tells u end of the string
    in the above for loop what i am doing is i start the loop at the str[0] and i will go all through the string till i get '\0'

    s.s.harish

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    There is a problem with your program harish. If the user was to enter "RaCe CaR" it will say that its not a palindrome. But it is! That's why it's neccessary to convert it all to lowercase or uppercase. How would I do that with fgets?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    184

    Smile

    yes the program what i have posted dosn't work for that

    change the if statment to as follow
    Code:
     if(toupper(str[k])!=toupper(str[j]))
    and try it. it works fine now. and more over the string "RaCe CaR" --> the answer is correct it is not a pallandrom

    check out the attached paint file i have explained it


    s.s.harish

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    in the previous prog it was checking E which the space
    that is why it was giving you as not a pallandrom

    u will have to eliminate the space in this case

    s.s.harish

  12. #12
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Quote Originally Posted by ssharish
    in the previous prog it was checking E which the space
    that is why it was giving you as not a pallandrom

    u will have to eliminate the space in this case

    s.s.harish
    what function can I use to autonatically remove all spaces entered by the user?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    there is no any function to eleminate the space. but u can check for is space or not using isspace() function.

    s.s.harish

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    "race car" is a palindrome. Ignoring spaces, it looks exactly the same forwards as it does backwards.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM