Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Palindrome problem 2

Status
Not open for further replies.

dgboult

Programmer
Apr 29, 2001
7
US
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(&quot;Enter a message: &quot;);
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(&quot;Not a Palindrome&quot;);
return 0;
}
}
printf(&quot;Palindrome\n&quot;);
printf(&quot;%s&quot;, str);
return 0;
}
 
Hi!!!
So going on with great enthu on learning C, U choose good problems for practice try to give atleast 2-3 days to develop logic don't go for getting easy answers. If you will decide not to find the answer from somone else unless it has taken painfully more no. of days or u r unsure of the syntax. This will help u to build up your logic. I think u should give one more thought I won't provide u with the source code but I can tell u how to go for it.


U have already converted all alphabets from lower to upper, now just read one character of this string per time, if its an alphabet store it in another string else leave it.


//////********Try not to read this before u have tried********//
I wish to say After conversion to all capitals-
for(j=0,i=0;i<strlen(str1);i++)
{
if(str>=65 && str<=90)
{
StringWithAlphaOnly[j]=str;
j++;
}
}
StringWithAlphaOnly[j]='\0';

Now here apply ur logic to check the Palindrome..... yes there is one more way to test it within the for loop of checking the palindrome. Try it on your own.

Happy Learning,
:)
SwapSawe.

 
The easiest way to do this would be to follow the following algorithm.

1. Read the string into a character array(say array1[])
You can use an array of known size or malloc() out the
memory you want from the system.

2. Create another array of the same size(say array2[])
3. In a for loop, do the following.

a. If the character is not punctuation mark and not space-like character, then copy it to the second array else skip it.

b. Come out of the loop.

4. Right now your string in array2 would be something like
&quot;poop&quot;;

array2[] = &quot;helivedasadevileh&quot;;

5. In a for loop do the following.
(size is the strlen(array2))
for (i=0; i<size/2; i++)
{
if ( array2 != array2[size -i -1] )
break;
}

6. Finally if i is equal to size/2 then the string
is a palindrome, else it is not.


Here is some sample code I wrote.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>


void main()
{

char array1[100];
char c, *array2;
int i, len, count;

count = 0;
printf(&quot;Enter a string: &quot;);
while ( (c = getchar()) != '\n')
array1[count++] = c;

array1[count] = '\0';
len = strlen(array1);

array2= (char *)malloc(sizeof(char)*(len+1));

count=0;
for (i=0; i<len; i++)
{
if (!ispunct(array1) && !isspace(array1))
array2[count++] = array1;
}

array2[count++] = '\0';
len = strlen(array2);

for(i=0; i<len/2; i++)
{
if (array2 != array2[len -i -1])
break;
}

if ( i == len/2 )
printf(&quot;String is palindrome\n&quot;);
else
printf(&quot;String is not a palindrome\n&quot;);
}

Hope this helps;

abp:)












 
Hi there

There is another simple solution to this problem,
which uses the power of recursion . COuldnt help admiring
the beauty of the solution when I saw the following code
in some page(dont remember the site). I am posting it
here.

// Palindrome problem solved using
// recursionint palindrome(char *str);

void main()
{

char array1[100];
char c, *array2;
int i, len, count;

count = 0;
printf(&quot;Enter a string: &quot;);
while ( (c = getchar()) != '\n')
array1[count++] = c;

array1[count] = '\0';
if ( palindrome(array1))
printf(&quot;String is palindrome\n&quot;);
else
printf(&quot;String is not a palindrome\n&quot;);
}

int
palindrome( char *str )
{
int len;
int ii;
int flag=1;

len = strlen(str);

if ( len == 1 ) return 1;
if ( str[0] == str[len-1] )
{
/* trim the last and first characters of the string */
str[len-1]='\0';
return palindrome(++str);
}
else
return 0;
}


Regards

Anand ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top