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! - #include <stdio.h>
-
#include <string.h>
-
-
#define TRUE 1
-
#define FALSE 0
-
-
-
void make_copy_of_string(char str[], char str_copy[]); //copies the string
-
void keep_chars(char string[]); //reads nothing but letters
-
void convert_upper_to_lower(char string[]); //covert upper case to lower
-
_Bool palindromeness(char string[]);
-
-
-
int main (void)
-
{
-
char phrase[101], phrase_copy[101];
-
-
printf("Enter a Phrase for palindrome checking: ");
-
fgets(phrase, 101, stdin); //reads first 100 letters
-
-
make_copy_of_string(phrase, phrase_copy); //makes copy of the phrase
-
keep_chars(phrase_copy); //keeps only letters
-
convert_upper_to_lower(phrase_copy); //convert
-
-
-
if(palindromeness(phrase_copy) == TRUE )
-
printf("Your phrase is: %s\nAnd it is a palindrome!\n", phrase);
-
-
else
-
printf("Your phrase is not a palindrome!\n", phrase);
-
-
return 0;
-
}
-
-
-
void make_copy_of_string(char str[], char str_copy[]) //make_copy_of_string function
-
-
{
-
int i=0;
-
-
while (str[i] != '\n' && str[i] != '\0')
-
{
-
str_copy[i] = str[i];
-
i++;
-
}
-
str_copy[i] = '\0';
-
str[i] = '\0';
-
-
}
-
-
void keep_chars(char string[]) //keep_chars function
-
{
-
int i=0, j=0;
-
-
while (string[i] != '\0')
-
{
-
-
if( ('A'<=string[i] && string[i] <= 'Z') || ('a'<=string[i] && string[i] <= 'z') )
-
{
-
string[j] = string[i];
-
i++;
-
j++;
-
}
-
else
-
{
-
i++;
-
}
-
-
}
-
-
string[j] = '\0'; //add terminating NULL
-
-
}
-
-
void convert_upper_to_lower(char string[]) //convert_upper_to_lower function
-
{
-
-
int i;
-
-
if ( 'A'<=string[i] && string[i]<='Z');
-
{ i+32;
-
-
}
-
}
-
-
_Bool palindromeness (char str[])
-
{
-
char string[101];
-
-
strcpy(string,str);
-
-
int i, j;
-
char temp[100];
-
for(i=strlen(str)-1, j=0; i+1!=0; --i,++j)
-
{
-
temp[j]=str[i];
-
}
-
temp[j]='\0';
-
strcpy(str,temp);
-
-
if( strcmp(string, str)== 0)
-
return TRUE;
-
else
-
return FALSE;
-
}
2 1829
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.
This code: - void convert_upper_to_lower(char string[]) //convert_upper_to_lower function
-
{
-
-
int i;
-
-
if ( 'A'<=string[i] && string[i]<='Z');
-
{ i+32;
-
- }
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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:...
|
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");
...
|
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...
|
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...
|
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?
|
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....
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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
|
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...
| |