Thread: Filtering out all characters that aren't letters? (Palindrome)

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    5

    Filtering out all characters that aren't letters? (Palindrome)

    I'm having trouble figuring out how to filter out all non-letter characters from my program. Would you have to do something with the ASCII code or what?Also for some reason, my program says it's not a palindrome if I write "He lived as a devil, eh?" even though it clearly is.

    Here's my code so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
    
    
        char str1[60], str2[60];
    
    
        printf("Enter the string for palindrome check:");
        scanf("%s", &str1);
    
    
        strcpy(str2, str1);
        strrev(str2);
    
    
        if(strcmp(str1, str2)==0)
        {
            printf("%s is a palindrome.", str1);
        }
        else
            printf("%s is not a palindrome.", str1);
    
    
        return 0;
    }
    Any help is appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    C Library <ctype.h>
    Only copy characters that pass isalpha().

    Actually, you want
    - the original string
    - the original string containing only alpha
    - the reverse of that.

    Make variable names to that effect, instead of meaningless str1,str2.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    5
    Quote Originally Posted by Salem View Post
    C Library <ctype.h>
    Only copy characters that pass isalpha().

    Actually, you want
    - the original string
    - the original string containing only alpha
    - the reverse of that.

    Make variable names to that effect, instead of meaningless str1,str2.
    Would you recommend doing this even if my teacher hasn't taught us about alpha and isalpha?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I recommend looking up what isalpha does; then writing your own function called something like myisalpha that does the same thing as what isalpha does.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    5
    Alright. I'll give it a look. Thanks for the response!

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    5
    I'm a little confused as to what I'm supposed to put inside isalpha(). Is it something to do with the string or what?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the library isalpha() takes a single char as a parameter, and returns a truth value.

    Perhaps yours should do the same.

    Though TBH, I would suggest you complete your code using the library isalpha() first, just so you know the rest of the code works.
    So that when you implement your own myIsAlpha(), you'll know you've done it right if you still get the same final result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-11-2013, 08:44 AM
  2. Replies: 2
    Last Post: 09-11-2013, 11:12 PM
  3. Replies: 5
    Last Post: 09-28-2012, 07:08 AM
  4. they aren't teh
    By neandrake in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 11-21-2004, 09:42 AM
  5. Why aren't we all here 24/7?
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-30-2001, 10:49 PM

Tags for this Thread