Hello,
How would I change the program so that it does not read spaces and punc. marks? I would like the program to be able to read a sentnce like " He lived as a devil, eh?" and say that it is a Palindrome. Right now I got the program to change the spaces and punc. marks to '-'.So it can read sentence's like "he, eh". But if the comma is like "he e,h" it will read it as "he-e-h" and say that it is not a Palindrome. I know the problem is some were in the for loops. I'm very frustrated!!
Thanks alot!!
#include <stdio.h>
#include <string.h>
/****************************************/
/* This program reads in a message then */
/* prints if it is a Palindrome or not. */
/****************************************/
int main(void)
{
char str[100];
int a,c;
/*************************/
/* Reads in the message. */
/*************************/
printf("Enter a message: "
for(a = 0; a < 100; a++) {
str[a] = getchar();
if(str[a]=='\n'){
str[a] = '\0';
break;
}
}
/*****************************************************************/
/* Converts all the letters to capitals unless they already are. */
/* Also they get rid of the spaces and puncuation marks. */
/*****************************************************************/
for(a=0; a<strlen(str); a++)
if(str[a]>=97 && str[a]<=122)
str[a]-=32;
for(a=0;a<strlen(str);a++)
if(ispunct(str[a]))
str[a]++;
for(a=0;a<strlen(str);a++)
if(isspace(str[a]))
str[a]='-';
/************************************************/
/* Calculates and prints if it is a Palindrome. */
/************************************************/
c = strlen(str)-1;
a = 0;
for(;a < strlen(str); a++, c--) {
if(str[c] != str[a]){
printf("Not a Palindrome"
return 0;
}
}
printf("Palindrome\n"
printf("%s", str);
return 0;
}
How would I change the program so that it does not read spaces and punc. marks? I would like the program to be able to read a sentnce like " He lived as a devil, eh?" and say that it is a Palindrome. Right now I got the program to change the spaces and punc. marks to '-'.So it can read sentence's like "he, eh". But if the comma is like "he e,h" it will read it as "he-e-h" and say that it is not a Palindrome. I know the problem is some were in the for loops. I'm very frustrated!!
Thanks alot!!
#include <stdio.h>
#include <string.h>
/****************************************/
/* This program reads in a message then */
/* prints if it is a Palindrome or not. */
/****************************************/
int main(void)
{
char str[100];
int a,c;
/*************************/
/* Reads in the message. */
/*************************/
printf("Enter a message: "
for(a = 0; a < 100; a++) {
str[a] = getchar();
if(str[a]=='\n'){
str[a] = '\0';
break;
}
}
/*****************************************************************/
/* Converts all the letters to capitals unless they already are. */
/* Also they get rid of the spaces and puncuation marks. */
/*****************************************************************/
for(a=0; a<strlen(str); a++)
if(str[a]>=97 && str[a]<=122)
str[a]-=32;
for(a=0;a<strlen(str);a++)
if(ispunct(str[a]))
str[a]++;
for(a=0;a<strlen(str);a++)
if(isspace(str[a]))
str[a]='-';
/************************************************/
/* Calculates and prints if it is a Palindrome. */
/************************************************/
c = strlen(str)-1;
a = 0;
for(;a < strlen(str); a++, c--) {
if(str[c] != str[a]){
printf("Not a Palindrome"
return 0;
}
}
printf("Palindrome\n"
printf("%s", str);
return 0;
}