Thread: Help with input conversion.....

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    8

    Help with input conversion.....

    hi all, i need to accept a string input, and convert the ouput into a string that consists of only the alphabets typed before in lowercase, and in ascending order. for example, user types in 4ASf4a8!@#jt, and the output is supposed to be aafjst. i dont know how to convert the alphabets into lowercase......can anyone help? thanks!!!

    Code:
    void sort()
    {
    	int count;
    	char line[20];
    	printf("Enter a string (1-20 characters):");
    	fflush(stdin);
    	gets(line);
    	for (count=0;count<strlen(line);count++)
    	{
    		if (int(line[count])>65)
    		{
    			//i come here = i'm a letter!!
    		}
    	}
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Don't use fflush(stdin);
    Don't use gets()

    Now that that's out of the way, you can use tolower() from ctype.h to convert a character from uppercase to lowercase.

    This looks like an error: if (int(line[count])>65)
    Where you trying to typecast line[count]? Right now you're calling a function called int() and passing line[count] to it.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    i'm trying to use (int(line[count])>65) so that i can check which inputs are alphabets by using the ASCII table code.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by gundamhamtaro
    i'm trying to use (int(line[count])>65) so that i can check which inputs are alphabets by using the ASCII table code.
    Just use isalpha() from ctype.h. It will return 1 if the character is alphabetic. You can use islower() if you want to see if it's lowercase.

    Have you actually tried to compile (int(line[count])>65)?
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    yeah, it gave me an "parse error before int" error......

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    ok, heres what i have now. i still dont know how to check for the correct positioning of the letters though. how do i do that? will strcmp help?
    Code:
    void sort()
    {
    	int count,count2;
    	char line[20],line2[20];
    	printf("Enter a string (1-20 characters):\n");
            scanf("%s",&line);
    	for (count=0;count<strlen(line);count++)
    	{
    		if (isalpha(line[count]) > 0)
    		{
    			//i come here = i'm a letter!!
    			if (islower(line[count]) != 1)
                               //if its not lowercase, change it to lowercase
      			   line2[count2] = tolower(line[count]);
                            else
      			   line2[count2] = line[count];
    			count2++;
    		}
    	}
    	printf("Output:\n");
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, your checks are not the greatest. For one, there's no guarintee that isalpha returns > 0 on success. It's just "not zero". That means the implementation could return a negative number if it felt like it. Plus, it's just extra code you don't really even need. The same goes for your islower check. There is no guarintee that it will in fact return one. It's just "non zero", or zero. Furthermore, since you're testing for upper case, why don't you just use isupper?
    Code:
    if( isalpha( line[ count ] ) )
    {
         if( isupper( line[ count ] ) )
        {
            ...
        }
        ...
    }
    Or, better yet, why even bother checking for isupper or islower at all?
    Code:
    for (count=0;count<strlen(line);count++)
    {
        if( isalpha( line[ count ] ) )
        {
            line2[ count2 ] = tolower( line[ count ] );
            count2++;
        }
    }
    Then when you've got a string with only lower case letters, sort it.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    thanks quzah, but how do i sort alphabets? any hints?

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Hint: if('a' > 'b')
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    ok thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am BRAND new to this, so please go easy on me
    By christianne in forum C Programming
    Replies: 12
    Last Post: 03-30-2006, 07:58 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  4. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM