473,320 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Palindrome identifierrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr rrrrrrrrrrrrrrrrrrrrrrrr

Hello, This is my code. I need to create a program where the user can enter a phrase (up to 100 characters), and the program will check if it is a Palindrome. I wrote the code and it works fine with 1 exception. It tells me that phrase is not a palindrome If I type: "Bob bob" "Lemon nomel", where first letter is upper case and last letter is lower case.DOnt know what to do. thats the code. Heeeeeeelp!


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define TRUE  1
  5. #define FALSE 0
  6.  
  7.  
  8. void make_copy_of_string(char str[], char str_copy[]);          //copies the string
  9. void keep_chars(char string[]);                                 //reads nothing but letters
  10. void convert_upper_to_lower(char string[]);                     //covert upper case to lower
  11. _Bool palindromeness(char string[]);
  12.  
  13.  
  14. int main (void)
  15. {
  16.         char phrase[101], phrase_copy[101];
  17.  
  18.         printf("Enter a Phrase for palindrome checking: ");
  19.         fgets(phrase, 101, stdin);                                      //reads first 100 letters
  20.  
  21.         make_copy_of_string(phrase, phrase_copy);                       //makes copy of the phrase
  22.         keep_chars(phrase_copy);                                        //keeps only letters
  23.         convert_upper_to_lower(phrase_copy);                            //convert
  24.  
  25.  
  26.         if(palindromeness(phrase_copy) == TRUE )
  27.            printf("Your phrase is: %s\nAnd it is a palindrome!\n", phrase);
  28.  
  29.         else
  30.            printf("Your phrase is not a palindrome!\n", phrase);
  31.  
  32.         return 0;
  33. }
  34.  
  35.  
  36.         void make_copy_of_string(char str[], char str_copy[])           //make_copy_of_string function
  37.  
  38. {
  39.            int i=0;
  40.  
  41.         while (str[i] != '\n' && str[i] != '\0')
  42. {
  43.            str_copy[i] = str[i];
  44.            i++;
  45. }
  46.            str_copy[i] = '\0';
  47.            str[i]      = '\0';
  48.  
  49. }
  50.  
  51.         void  keep_chars(char string[])                         //keep_chars function
  52. {
  53.            int  i=0, j=0;
  54.  
  55.         while (string[i] != '\0')
  56. {
  57.  
  58.         if(  ('A'<=string[i] && string[i] <= 'Z') || ('a'<=string[i] && string[i] <= 'z') )
  59. {
  60.           string[j] = string[i];
  61.           i++;
  62.           j++;
  63. }
  64.         else
  65. {
  66.           i++;
  67. }
  68.  
  69. }
  70.  
  71.         string[j] = '\0';                                       //add terminating NULL
  72.  
  73. }
  74.  
  75.        void convert_upper_to_lower(char string[])               //convert_upper_to_lower function
  76. {
  77.  
  78.         int i;
  79.  
  80.         if ( 'A'<=string[i] && string[i]<='Z');
  81. {          i+32;
  82.  
  83. }
  84. }
  85.  
  86.         _Bool palindromeness (char str[])
  87. {
  88.         char string[101];
  89.  
  90.         strcpy(string,str);
  91.  
  92.            int i, j;
  93.            char temp[100];
  94.            for(i=strlen(str)-1, j=0; i+1!=0; --i,++j)
  95. {
  96.         temp[j]=str[i];
  97. }
  98.         temp[j]='\0';
  99.         strcpy(str,temp);
  100.  
  101.         if( strcmp(string, str)== 0)
  102.           return TRUE;
  103.         else
  104.           return FALSE;
  105.  }
Feb 11 '15 #1
2 1829
donbock
2,426 Expert 2GB
Take a look at isalpha(), toupper(), and tolower() in ctype.h.

The C Standard allows use of character encodings other than ASCII. Some non-ASCII character encodings do not assign contiguous values to the letters of the alphabet. Thus, tests like those on lines 58 and 80 are not portable. Use isalpha() instead.

Non-ASCII character encodings may have different relationships between the values of upper- and lowercase characters. Thus, conversions like the one at line 81 are not portable. Use toupper() or tolower() instead. These standard conversion functions return the input value if it isn't one that needs to be converted, so you don't need to test the values yourself as you did on line 80.

You strip all non letters from the string before testing if it is a palindrome. Maybe you only want to strip white space from the string ... If you want "Bob4..4bob" to be considered a palindrome.
Feb 12 '15 #2
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1.        void convert_upper_to_lower(char string[])               //convert_upper_to_lower function
  2. {
  3.  
  4.         int i;
  5.  
  6.         if ( 'A'<=string[i] && string[i]<='Z');
  7. {          i+32;
  8.  
  9. }
  10.  
The semi-colon at the end of the if terminates the if-statement without doing anything. Also, i+32 doesn't do anything either.

Worse, the int i is used without an initial value.
Feb 12 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Lorin Leone | last post by:
Can anyone help me modify the program so that it recognizes strings like "Anna" as palindromes. To make the program "case-insensitive." using the built-in C++ function "toupper". and so that it...
23
by: Amar Prakash Tripaithi | last post by:
Dan Hoey, who had recently graduated, wrote a C program to look for and construct the following beauty: A man, a plan, a caret, a ban, a myriad, a sum, a lac, a liar, a hoop, a pint, a catalpa,...
32
by: ramakrishnadeepak | last post by:
HI Everybody, I 've to submit a program on c.Can any one help me plz.........The problem is like this:: Write a program which computes the largest palindrome substring of a string. Input:...
4
by: outofmymind | last post by:
hi, im trying to solve the following question: Create a class responsible for determining whether a string is a palindrome. Show your test cases. Palindome mypal("bob"); ...
3
by: colinNeedsJavaHelp | last post by:
I am still having an exceptional amount of trouble with java. This is my new assignment, if anyone can help I would greatly appreciate it. I don't even know where to start. A word or phrase in...
2
by: Synapse | last post by:
aloha people! I need help in my java palindrome program. It's a 5-digit palindrome checker. The code below is running good but i got a few problems on it. If I enter 33633, 11211, 45554, it will...
20
by: Wabz | last post by:
Hello mates, Does anyone know how to write a function that tests if an integer is a palindrome in C language?
2
by: bigtd08 | last post by:
help writing this palindrome program for my c++ class. HERE WHAT THE CODE SHOULD BE LIKE. Write a program that takes a line of input from the keyboard and check to see if that line is a palindrome....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.