anything to overwrite a char array?

This is a discussion on anything to overwrite a char array? within the C Programming forums, part of the General Programming Boards category; i was just wondering how to fix my program... If user wants to enter another string (e.g. shorter one), some ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    37

    anything to overwrite a char array?

    i was just wondering how to fix my program... If user wants to enter another string (e.g. shorter one), some characters are left in the array, and the count of vowels, consonants and white spaces is not accurate. Is there a way to --- i don't know -- count the chars in the array? Without using anything from ctype.h? What can I do?

    thanks...
    Attached Files Attached Files

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Firstly, dont use gets(), use fgets().

    Seeing that fgets(), will have a '\n' at the end, you could loop up to the first '\n', and then you'll be at the end of the string.

    Code:
    //Change this :
    for (i = 0; i <= 511; i++)
    
    //To this :
    for ( i = 0; inputstring[i] != '\n'; i++ )

  3. #3
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    In order to count vowels use something like:
    Code:
    char vowels[] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
    
    for ( i = 0; i < 10; i++)
       if ( ch == vowels[i] )
          ch_is_a_vowel = 1;
    If ch is inside the characters range but it is not a vowel, then it must be a consonant )

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Code:
    char vowels[] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
    know that

    Code:
    char vowels[] = "aeiouAEIOU"
    is easier to write and does almost exactly the same thing (the exception being it adds a '\0' to the end)
    hello, internet!

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    Ok, I can do that. Thanks. Actually it was the first thing I thought of before I started writing this... I can change this, no problem.
    Still, what should I do with the inputarray, which is filled and a next, shorter string will not overwrite the rest of the previous one?

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    I did try to fill the new array with '\0', but it still doesn't work. The new string is located at the beginningof the array, but the older string is still right after the new one....
    What is going on? What did I do wrong this time?

    Code:
    # include <stdio.h>
    
    
    
    int main()
    {
    
    
    	char again = 'y';
    	char inputstring[112] = "\0";
    	char *inputptr;
    	int i = 0;
    	int count_vow = 0;
    	int count_cons = 0;
    	int count_white = 0;
    	int count_punct = 0;
    	int non_counted = 0;
    	int tot_wh_sp_ct = 0;
    	inputptr = inputstring;
    	
    	
    	
    	do
    	{
    		{
    		printf("Enter an English text. Use at least one character and no more than 111 characters, including white spaces.\n");
    		gets(inputstring);
    		printf("The text you entered was:\n");
    		printf("%s", inputstring);
    		
    		for (i = 0; i<=111; i++)
    		{
    			switch(inputstring[i])
    			{
    			case 'a': 
    			case 'A': 
    			case 'e': 
    			case 'E': 
    			case 'i': 
    			case 'I': 
    			case 'o': 
    			case 'O': 
    			case 'u': 
    			case 'U': count_vow++;
    			
    			default: non_counted++;
    			}
    
    			switch(inputstring[i])
    			{
    				case 'b': 
    				case 'B': 
    				case 'c': 
    				case 'C': 
    				case 'd': 
    				case 'D': 
    				case 'f': 
    				case 'F': 
    				case 'g': 
    				case 'G': 
    				case 'h': 
    				case 'H':
    				case 'j': 
    				case 'J': 
    				case 'k': 
    				case 'K': 
    				case 'l': 
    				case 'L': 
    				case 'm': 
    				case 'M': 
    				case 'n': 
    				case 'N': 
    				case 'p': 
    				case 'P': 
    				case 'q': 
    				case 'Q': 
    				case 'r': 
    				case 'R': 
    				case 's':
    				case 'S': 
    				case 't': 
    				case 'T': 
    				case 'v': 
    				case 'V': 
    				case 'x': 
    				case 'X':
    				case 'y':
    				case 'Y':
    				case 'z':
    				case 'Z': count_cons++;
    
    				default: non_counted++;
    			}
    
    			switch(inputstring[i])
    			{
    			case ' ':
    			case '\t': count_white++;
    			default: non_counted++;
    			}
    
    			switch(inputstring[i])
    			{
    			case '.':
    			case ';':
    			case ',':
    			case '!':
    			case '?': count_punct++;
    			default: non_counted++;
    			}
    
    		}
    		tot_wh_sp_ct = count_white + 1;
    		printf("\nand it contained %d consonants, %d vowels, %d white spaces, %d punctuation marks.",count_cons, count_vow, tot_wh_sp_ct, count_punct);
    		}
    
    	for (i=0; i<=111; i++)
    	{
    		inputstring[i] = '\0';
    	}
    
    	
    	printf("\nWould you like to enter another text?");
    	again = getchar();
    	while ( getchar() != '\n' ) 
    	continue;
    	}
    	while (again == 'y' || again =='Y');
    
    	return 0;
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I haven't read all of your program, but in answer to your question:
    what should I do with the inputarray, which is filled and a next, shorter string will not overwrite the rest of the previous one?
    ... What is the problem here? OK, so you're array isn't completely filled with a new string, but that doesn't matter. you know where the new string stops, because it's last character is \0. Anything after that is irrelevant. Does this help you?

    Alternatively, is you're that worried, fill the array with \0 before asking the user for a new string.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    Oh. I finally know where the problem wasis. It's not the string array. It is the variables which stay after one run of the program. If I do not exit and re-execute the program they will be adding new values to the previous (from the previous string)....
    I filled in the array with '\0', just in case.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

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