Thread: Double Matches

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

    Double Matches

    How can i make it so it doesn't output the same word more than once? And to make it go back to the entering input until they press CTRL+Z as the input?

    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;
    	}
    }
    
    
    char c;
    
    int main()
    {
    	int LowUpp(int ch);
    
    	char word[23];
    	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("Word & Character Counter\n\n\n");
    	printf("Enter your input ([CTRL+Z] To Quit): ");
    
    	if(fgets(userstring, 1024, stdin) == NULL)
    		return 0;
    
    	printf("\n\nWord Count		");
    	printf("Word			");
    	printf("Character Count	\n");
    	printf("---------------------------------------------------------------\n");
    
    	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];
    
    			if(charcount < 8)
    				printf("%d\t\t\t%s\t\t\t%d\n", wordnumb, word, charcount);
    			else
    				printf("%d\t\t\t%s\t%d\n", wordnumb, word, charcount);
    
    			charcount = 0;
    			wordcount++;
    			j = 0;
    		}
    			i++;
    	}
    	
    	printf("---------------------------------------------------------------\n");
    	printf("Total Words:  %d\n\n", wordcount);
    	
    	return 0;
    }

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Code:
    
    
    int LowUpp(int ch)
    {
    	if ( 'a' <= ch && ch <= 'z' )
    	{
    		return (ch + 'A' - 'a');
    	}
    	else
    	{
    		return ch;
    	}
    }

    Why" reinvent the wheel " here. Use one of the character classification functions (toupper or tolower) found in <ctype.h>
    Mr. C: Author and Instructor

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    What OS is this on? If you plan ondoing Ctrl+Z in Linux, you need to setup a signal handler I beleive. Ctrl+Z is used for suspending and application and you want to avoid that.

    (someone, correct me if i'm wrong please)

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Windows. And for the assignment I can only use stdio.h So i had to do that function.

  5. #5
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Originally posted by orbitz
    What OS is this on? If you plan ondoing Ctrl+Z in Linux, you need to setup a signal handler I beleive. Ctrl+Z is used for suspending and application and you want to avoid that.

    (someone, correct me if i'm wrong please)
    Windows uses CTRL-Z ...*nix uses CTRL-d <return>

    Corrected
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>how can i make it so it doesn't output the same word more than once?
    Store the words in a 2-d array, and before you print each one, check the array to see if your word is in there. You need a seperate function to do the checking. Just pass it the 2-d array name and the char array holding the need word, and get it to loop through the 2-d array doing strcmp()'s on each entry.

    >make it go back to the entering input until they press CTRL+Z as the input?
    You need a while loop, like this:
    Code:
    while (fgets(buf, sizeof buf, stdin) != NULL)
    {
        /* Process words here */
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Store the words in a 2-d array, and before you print each one, check the array to see if your word is in there.
    For added fun, use a hash table instead! Maybe you'll get extra credit.

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

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Originally posted by datainjector
    Windows uses CTRL-Z ...*nix uses CTRL-d <return>

    Corrected
    If you are talkinga bo end of file then you are mostly right (no <return in linux>
    I was talking about suspending the applicaiton, inwhichc ase ^Z is it in linux, but thanks for the clarification.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM