populating a string only alpha characters

This is a discussion on populating a string only alpha characters within the C Programming forums, part of the General Programming Boards category; I am a C student (intermediate i think). I am having difficulties populating a string with only alpha characters. I ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39

    populating a string only alpha characters

    I am a C student (intermediate i think). I am having difficulties populating a string with only alpha characters. I have tried using a combination of gets() and isalpha() with no success. If this method is possible then I cant seem to nail down the logic. I have tried using isalpha(*str) in main and in the function definition itself with no success. Can someone point me in the right direction?
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #define _CRT_SECURE_NO_DEPRECATE 
    #define MAX 61
    
    int isPalindrome(char* str);
    void printResults(char* str, int answer);
    
    int main()
    {
    	char str[MAX];
    	int answer;	
    	puts("Enter a string");		
    	gets(str);
    	
    	answer = isPalindrome(str);
    	printResults(str, answer);	
    }
    
    /*************************** FUNCTION ***************************
     * isPalindrome()
     * This takes only the alpha characters of a given string,
     * converts them into all upper case and returns 1 if 
     * the string is a palindrome
     *************************** FUNCTION ***************************/
    int isPalindrome(char* str)
    {
            char* end = str + strlen(str);
    	int i;
    	
    	for (i = 0; i < strlen(str); i++)
    	{
    		
    		str[i] = toupper(str[i]);
    	}
    		
    	while (end > str)
    	{
    		if (*str++ != *--end)
    		{
    			return 0;
    		}
    	}
    	return 1;
    }
    
    /*************************** FUNCTION ***************************
     * printResults()
     * This string prints the string entered by the user and the 
     * awnswer to whether or not it is a palindrome
     *************************** FUNCTION ***************************/
    void printResults(char* str, int answer)
    {
    	if (answer == 1)
    	{
    		printf("%s is a palindrome\n", str);
    	}
    	else
    	{
    		printf("%s is NOT a palindrome\n", str);
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    isalpha works on a character, so presumably you would do that at the same time you do toupper (which also works on a character). If you want to check reversal without the non-alphabetic characters, you will probably want to build a separate string, just inside the function, that contains all the letters and none of the other stuff.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    you don't have to populate the string with only alphas, you just have to ignore the non-alphas in the palindrome test.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int ispalindrome(char *s);
    
    int main()
    {
        char s[80];
        printf("enter a string> ");
        gets(s);
        printf("'%s' %s a palindrome\n", s, ispalindrome(s) ? "is" : "is not");
        return EXIT_SUCCESS;
    }
    
    int ispalindrome(char *s)
    {
        int i, j;
        for (i = 0, j = strlen(s)-1; i < j; i++, j--)
        {
            /* ignore non-alphas */
            while (!isalpha(s[i])) i++;
            while (!isalpha(s[j])) j--;
            /* case insensitive test the alphas */
            if (toupper(s[i]) != toupper(s[j])) return 0;
        }
        return 1;
    }

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Maybe something more like:

    Code:
    char *in = str;
    char *out = str;
    char *end = str + strlen(str);
    while(in < end)
    {
        if(isalpha(*in))
            *out++ = toupper(*in);
        in++;
    }
    The code which checks for the palindome looks okay.

    And if this were commercial code, I'd add a comment noting that this function destroys its input (it alters the string which was passed to it) and that passing it a string literal is a no-no.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Quote Originally Posted by Meldreth View Post
    you don't have to populate the string with only alphas, you just have to ignore the non-alphas in the palindrome test.
    Code:
    int ispalindrome(char *s)
    {
        int i, j;
        for (i = 0, j = strlen(s)-1; i < j; i++, j--)
        {
            /* ignore non-alphas */
            while (!isalpha(s[i])) i++;
            while (!isalpha(s[j])) j--;
            /* case insensitive test the alphas */
            if (toupper(s[i]) != toupper(s[j])) return 0;
        }
        return 1;
    }
    Thanks! This works perfectly!
    Question... I understand the while statements. Basically if there is a non-alpha character in either direction, the indexer jumps over it and initializes to the next value. But can you explain how the if() statement works?

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by MSF1981 View Post
    Thanks! This works perfectly!
    Question... I understand the while statements. Basically if there is a non-alpha character in either direction, the indexer jumps over it and initializes to the next value. But can you explain how the if() statement works?
    Have you tested this code with the string that contains only 2 spaces and nothing more?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Don't use gets.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Quote Originally Posted by vart View Post
    Have you tested this code with the string that contains only 2 spaces and nothing more?
    It works just fine using Xcode... However I haven't tested it in Visual Studio yet. I'll have to reboot into my windows partition to give that a shot... Stay tuned.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by MSF1981
    But can you explain how the if() statement works?
    both of the characters are converted to upper case and if they're not equal, it's not a palindrome. as vart hinted, it's just an example and not perfect. use my code as a start for your code, not the full solution.

  10. #10
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    You could specify a scanset for scanf, that would simplify the problem. If you design the program cleverly enough it would make it resilient as well.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  11. #11
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Quote Originally Posted by Meldreth View Post
    both of the characters are converted to upper case and if they're not equal, it's not a palindrome.
    palm/forehead Duh!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Reading characters from a string?
    By ladysniper in forum C++ Programming
    Replies: 6
    Last Post: 04-08-2006, 11:45 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21