Hello,
I wrote this program to see if a message is a Palindrome or not. Like "He lived as a devil, eh?". My question is how can I use pointers to keep track of the current position in the array?
#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,j;
/*************************/
/* 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, */
/* then gets rid of the spaces and punctuation marks. */
/*****************************************************************/
for(a=0; a<strlen(str); a++)
if(str[a]>=97 && str[a]<=122)
str[a]-=32;
for(j=0,a=0; a<strlen(str);a++)
{
if(str[a]>=65 && str[a]<=90)
{
str[j]=str[a];
j++;
}
}
str[j]='\0';
/************************************************/
/* Calculates and prints if it is a Palindrome. */
/************************************************/
c = strlen(str)-1;
for(a=0; a < strlen(str); a++, c--) {
if(str[c] != str[a]){
printf("Not a Palindrome"
return 0;
}
}
printf("Palindrome"
return 0;
}
Thanks!!
I wrote this program to see if a message is a Palindrome or not. Like "He lived as a devil, eh?". My question is how can I use pointers to keep track of the current position in the array?
#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,j;
/*************************/
/* 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, */
/* then gets rid of the spaces and punctuation marks. */
/*****************************************************************/
for(a=0; a<strlen(str); a++)
if(str[a]>=97 && str[a]<=122)
str[a]-=32;
for(j=0,a=0; a<strlen(str);a++)
{
if(str[a]>=65 && str[a]<=90)
{
str[j]=str[a];
j++;
}
}
str[j]='\0';
/************************************************/
/* Calculates and prints if it is a Palindrome. */
/************************************************/
c = strlen(str)-1;
for(a=0; a < strlen(str); a++, c--) {
if(str[c] != str[a]){
printf("Not a Palindrome"
return 0;
}
}
printf("Palindrome"
return 0;
}
Thanks!!