Thread: Lower to Upper

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    57

    Post Lower to Upper

    where do i insert the LowUpp(); function into my main(); to get it to make the lowercase letters into uppercase?

    Code:
    #include <stdio.h> 
    
    int LowUpp(int ch)
    {
    	if ( 'a' <= ch && ch <= 'z' )
    	{
    		return (ch + 'A' - 'a');
    	}
    	else
    	{
    		return ch;
    	}
    }
    
    int main() 
    {
    	int LowUpp(int ch);
    	char word[50]; 
    	char userstring[1024]; 
    	int wordcount = 0; 
    	int charcount = 0;
    	int i = 0; 
    	int j = 0; 
    
    	printf("Enter your input: "); 
    
    	if(fgets(userstring, 1024, stdin) == NULL) 
    		return 0;
    
    	while(userstring[i] != '\0') 
    	{
    		if((userstring[i] != ' ') && (userstring[i] != '\n') && (userstring[i] != '\t'))
    		{
    			charcount++;
    
    			word[j++] = userstring[i]; 
    		}
    
    		else
    		{
    			word[j] = '\0';
    
    			printf("%s \n", word); 
    	
    			wordcount++;
    	
    			j = 0;
    		}
    
    		i++; 
    	} 
    
    	printf("There were %d words and %d characters.\n", wordcount, charcount); 
    
    	return 1;
    }

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Thanks.

    How do I make the thing count each word seperately?

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Krush
    Thanks.

    How do I make the thing count each word seperately?
    Loop through the string, and every time you see a "word break" character, increment the word count.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Oops I meant to count the characters of each word separately.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Krush
    Oops I meant to count the characters of each word separately.
    Similar kind of answer still. It now just depends on what you want to do with the counts (ie store them, print them etc).

    Loop through the string, incrementing the counter as you go past each letter, when you reach a word boundry, print out counter and reset it to zero.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Same way as Hammer suggested except you would increment the character count everytime you don't hit a "word break" character. Then you would reset that character count (store it or print it first if you must, depending on what you need to do), and count again until there are no words left.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Code:
    #include <stdio.h> 
    
    /* This function takes all the characters and makes them UPPERCASE */
    int LowUpp(int ch)
    {
    	if ( 'a' <= ch && ch <= 'z' )
    	{
    		return (ch + 'A' - 'a');
    	}
    	else
    	{
    		return ch;
    	}
    }
    
    int main()
    {
    	int LowUpp(int ch);
    
    	char word[50];
    	char userstring[1024];
    	char Array[1024][100];
    
    	int wordcount = 0;
    	int charcount = 0;
    	int wordnumb = 0;
    	int i = 0;
    	int j = 0;
    	int row = 0;
    	int column = 0;
    
    	printf("Enter your input: ");
    
    	if(fgets(userstring, 1024, stdin) == NULL)
    		return 0;
    
    	while(userstring[i] != '\0')
    	{ 
    		if((userstring[i] != ' ') && (userstring[i] != '\n') && (userstring[i] != '\t'))
    		{ 
    			charcount++;
    
    			word[j++] = LowUpp(userstring[i]);
    		}
    		else
    		{
    			wordnumb++;
    			word[j] = '\0';
    
    			Array[row][column] = word[i];
    
    			printf("%d %s %d \n", wordnumb, word, charcount);
    
    			charcount = 0;
    
    			wordcount++;
    
    			j = 0;
    		}
    
    		i++;
    	}
    
    	printf("There were %d words and %d characters.\n", wordcount, charcount);
    
    	return 1;
    }
    How do I put that in an array so it looks like this ?

    Code:
    Total Words: 6
    
    Word Count	Word			Character Count
    -------------------------------------------------------
    1		SEA				3
    2		TO				2
    3		SHINING				7
    4		C				1
    5		HELLO				5
    6		WORLD				5
    
    -------------------------------------------------------

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read this. In particular, Quzah's point number 6.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    but he leaves out stuff in middle.
    Code:
    for( x = 0; x < 100; x++ )
    {
        ...do some test to stop early...
    
        ...print stuff...
        printf("%03d\t%30s\t%03\n",
            x+1, /* the line number */
            myArray[x], /* array index */
            strlen( myArray[x] ), /*letter count of said index */
    }
    and I really need to know the part about how to put it in a array.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>and I really need to know the part about how to put it in a array.
    As I said to the other poster in that thread, you don't need an array if all you want to do is print that table. Quzah's code does that for you. ::: deja vu :::
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    My teacher wants a 2d array so i do need one.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #define MAX_WORDS 10
    #define MAX_LENGTH 50
    
    char MyArray[MAX_WORDS][MAX_LENGTH];
    There you go, a 2-d array that will hold all the words. Is that what you wanted?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Krush
    My teacher wants a 2d array so i do need one.

    ARRRRG! WHAT'S WRONG WITH YOU?? My exact line of code was written to use your 2D array! What more do you want? You do know how to print a string right? You print the array index it's in:

    array[] = one string

    printf("%s", array );

    array[][] = bunch of strings

    printf("%s", array[x] ); //print string x

    And so, our hero, once again, refers back to the original line of code I wrote you like a week ago which solves your problem. Why is it no one listens to me when I post the exact answer they need and keep referring back to it over and over telling them they're right?

    Why it is when you're stuck, and I've sworn up and down I've already given you the answer, do you insist on saying I'm wrong without even trying the single line of code that solves the damn problem?!?!

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

  14. #14
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    because doing it the hard way is so much more fun!!!
    guns dont kill people, abortion clinics kill people.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. to lower or to upper that is the question!
    By verbity in forum C++ Programming
    Replies: 20
    Last Post: 04-25-2007, 06:42 PM
  2. help...Lower and Upper case problem only
    By j4k50n in forum C Programming
    Replies: 9
    Last Post: 04-19-2007, 12:39 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. upper case to lower case problem
    By Jasonymk in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 05:35 AM
  5. lower to upper
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-29-2002, 07:50 PM