Thread: Ignoring specific characters in a string

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    24

    Ignoring specific characters in a string

    Hi,

    So I am writing a program that checks to see whether a string input by the user is a palindrome or not. Part of the problem involves ignoring any character that isn't a letter when verifying the string.

    I know that in C, strings are just arrays of characters. So my initial thought was to progress through the array and use something like isalpha() to determine whether or not the character is a letter. The issue is that I don't know how to ignore those characters when I compare the strings. Can anyone give me any insight?

    Thanks.

    -Sean

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    So is this a palindrome to you:
    Code:
    rad5ar
    since you are going to ignore the '5'?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    That's correct

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you could consider making a copy of your string, copying only those characters you wish to check with.
    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.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Quote Originally Posted by Salem View Post
    Well you could consider making a copy of your string, copying only those characters you wish to check with.
    My roommate just suggested that to me. It seems to make the most sense. I'm assuming I'd use something like strcpy(). But how would i keep from copying the unwanted characters?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by seanksg View Post
    My roommate just suggested that to me. It seems to make the most sense. I'm assuming I'd use something like strcpy(). But how would i keep from copying the unwanted characters?
    Don't use strcpy. Instead use a loop to go over the string character by character. For each character that is a letter, copy it into the new string.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Quote Originally Posted by bithub View Post
    Don't use strcpy. Instead use a loop to go over the string character by character. For each character that is a letter, copy it into the new string.
    Ah that makes more sense. Thanks! I'll try that out.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Yes, that's quite testable.

    Make a function that "RemovesNonLetters"

    Test it.

    Then make a function that tests for palindromism. ;-)

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Why copy anything. Test first character against last, as long as both are alpha. 2nd against 2nd last, etc. Keeping 2 indexes which will be moved towards each other.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <ctype.h>
    #include <string.h>
    
    #define TRUE 1
    #define FALSE 0
    
    int pal(char *input);
    
    int main( )
    {
    	char *stop = "done";
    	char input[81];
    
    	printf("\nEnter a string of characters for palindrome verification: ");
    	scanf("%80s", input);
    		
    	while (strcmp(input,stop) != 0)
    	{
    		printf("\n%s\n\n", input);
    		if (pal(input))
    		{
    			printf("Input is a palindrome\n\n");
    		}
    		else
    		{
    			printf("Input is not a palindrome");
    		}
    		printf("\n\nEnter a string of characters for palindrome verification: ");
    		scanf("%80s", input);
    	}
    
    	printf("\n******Program Terminated******\n\n");
    		
       	return (EXIT_SUCCESS);
    }
    int pal(char *input)
    {
    	int length = strlen(input) - 1;
    	int i = 0;
    	int j = length;
    	char start = input[i];
    	char end = input[j];
    	int exit = 0;
    
    	while (TRUE && (exit = 0))
    	{
    		while (isalpha(start) != 0)
    		{
    			++i;
    		}
    		while (isalpha(end) != 0)
    		{
    			--j;
    		}
    		if (i > j)
    		{
    			return (1);
    		}
    		else
    		{
    			while (isalpha(end) == 0 && isalpha(start) == 0)		
    			{
    				if (start == end)
    				{
    					++i;
    					--j;
    				}
    				else
    				{
    					exit = 1;
    				}
    			}
    		}
    	}
    
    	return (0);
    }
    This is the code I've developed. I decided to try comparing characters individually, because I couldn't figure out how to reverse the string. Nevertheless, it doesn't work. Does anyone have any insights?

    Thanks.

    -Sean
    Last edited by seanksg; 05-02-2011 at 05:48 PM. Reason: Forgot to add some information

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while head < tail 
        if !isalpha head
            head++
        else
        if !isalpha tail
            tail--
        else
        if head == tail 
            head++
            tail--
        else
            fail
    success
    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    while (TRUE && (exit = 0))
    You are doing an assignment here, not a comparison. You need to use the == operator.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ignoring certain characters in input
    By Mentallic in forum C Programming
    Replies: 2
    Last Post: 03-27-2010, 11:06 PM
  2. Identifying specific characters in a string
    By London Fog in forum C Programming
    Replies: 8
    Last Post: 08-14-2009, 12:59 PM
  3. Removing Specific Characters from Strings
    By gfmartin05 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2009, 09:53 AM
  4. Ignoring New Line Characters
    By Spencer Wallace in forum C Programming
    Replies: 2
    Last Post: 03-27-2006, 01:23 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

Tags for this Thread