Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

String help

  • 06-11-2007 04:45PM
    #1
    Closed Accounts Posts: 58 ✭✭


    Hey, I'm trying to get a program to take in a sequence of lines and replace each four letter word by asterisks. Here's my attempt
    #include<stdio.h>
    #include<string.h>
    
    #define SIZE 50
    int main(void)
    {
    	char text[SIZE];
    	int i, b, count=0;
    
    	puts("Enter text> ");	// get user input
    	gets(text);
    
    	b = strlen(text);
    
    	for(i=0;i<b;i++)	// loop throuh string
    	{
    		count++;
    
    		if(text[i]==32 || text[i]=='\0')	// when a space is encountered or end of the string
    		{				                    // check if word was four letters long
    			if(count==4)
    			{			// replace with asterisks
    				text[i-4] = '*';
    				text[i-3] = '*';
    				text[i-2] = '*';
    				text[i-1] = '*';
    			}
    
    			count=0;
    		}
    	}
    
    	printf("\n\n%s\n",text);
    }
    

    It won't alter the string at all and is confusing me. Is this the easiest approach or is there some string.h functions that could help me? Any help would be appreciated.


Comments

  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    I think you're going about this the wrong way. I assume your replacing all four letter words in a sentence with asterisks. The best way would be to split the string into words by first removing all punctuation marks (commas, full stops, exclamation marks etc.) then splitting the sentence on whitespace. You now have an array of words. Loop through the array and replace the contents of each array position with "****" where the length of the array position is 4.

    Hope that helps.
    RD


  • Closed Accounts Posts: 583 ✭✭✭monkey tennis


    ^^ the suggestion above is probably a better way of approaching this.

    You should, however, do some debugging so you realize what is going wrong with the method you're trying now. Debugging is very important in programming, and you should learn about it as soon as possible and get used to doing it (here's a good opportunity). Make sure you know exactly what's happening at every step of execution. You've made a small mistake that you've obviously overlooked by reading through the code; adding some debugging code would quickly point you in the right direction (debugging code being code that shows on-screen or in an error file exactly what the values of variables are at each step or iteration of a loop, etc).

    Here's a hint: What is 'count' counting? Is it counting more than you need? Add code to make the program echo out the value of 'count' and the character it's counting at each iteration of the for-loop. You'll soon spot what's wrong...


  • Closed Accounts Posts: 58 ✭✭Larry_o_C


    Gah! What a stupid little error! Thanks guys, got it working now.


Advertisement