Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

School exercise - c

Options
  • 31-10-2008 11:37pm
    #1
    Closed Accounts Posts: 815 ✭✭✭


    Hi all

    We are learning c at college and have been asked to write a program that takes a sequence of lines and displays each line with all four-letter words replaced by asterisks.

    Can anyone give me any pointers (excuse the pun) as to the best way to go about this.

    I am thinking I'll start with an array of strings (an array of char arrays) each read in with gets. Then I pass that array to a function which does the work.

    I need to find each word that is 4 characters long and replace it with '****'. That is the bit that I cannot figure out at all. Our latest lecture was on strings (strcopy, strncopy, strcat, strncat, strcmp) so I assume we need to be using those tools to do this puzzle.

    Can anyone help?, I have stared at this question for the past hour and cannot see a way forward.


Comments

  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    what I'd do to solve this (please bear in mind I'm coming home on the bus from a night on the drink) is to search through each line looking for two space characters and everything inbetween. I would store this into a temp var and then use string length to check the length of the string excluding the whitespaces. If its = 4 then replace with stars.

    There are easier ways to do this and I'd personally use a regular expression but I know some lecturers frown upon students using methods that aren't taught in class.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    You can split a string up using strtok (tokens, or delimiters would be your space) and then you'll get pointerer to every word. Then just check the size of it strlen to check its length. You can then joined all words back together again with *'s as your 4 letter ones.

    You will need string.h included for this but I amn't sure if that is allowed, perhaps you must do a more manual approach.


  • Closed Accounts Posts: 815 ✭✭✭KStaford


    yea I think we need a more manual approach. we did not cover strtok so we cannot use it.

    firstly, how would I find a 4 letter word?
    I am thinking maybe a for loop, checking the current char is a character (a-z or A-Z). If so then I am in a word so start counting until current char is no longer a character.
    If the counter = 4 then its a 4 letter word. Would you agree with this approach ?

    Secondly, while I'm counting the words, I could record the start and end positions for each word. In the case of 4 letter words, I could use these position counts as indixes for a strncopy i.e strcnopy (string, &a[start_pos], end_pos).

    What do you think folks? am I on the right track?


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    KStaford wrote: »
    I am thinking maybe a for loop, checking the current char is a character (a-z or A-Z). If so then I am in a word so start counting until current char is no longer a character.
    If the counter = 4 then its a 4 letter word. Would you agree with this approach ?

    Secondly, while I'm counting the words, I could record the start and end positions for each word. In the case of 4 letter words, I could use these position counts as indixes for a strncopy i.e strcnopy (string, &a[start_pos], end_pos).

    What do you think folks? am I on the right track?
    Yep that sounds good.

    The check for words should be more on whats not a space or tab character, and remember you may need to check for more than one of these characters (ie. multiple spaces separating words).


  • Closed Accounts Posts: 815 ✭✭✭KStaford


    this is what Ive come up with s far but it just will not work. been at it for hours and tearig my hair out a bit now
    Can anyone see what the heck Im doin wrong ?
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    #define NUM_LINES 4
    
    
    void main ()
    {
    
    	char arr[NUM_LINES][100]; //2d array to store multi line text
    	int i, j; //for loop controls
    	int cnt=0, start_pos=-1, end_pos=0;
    	int len=0;
    	char temp[NUM_LINES][100];  //store for processed text
    
    	strcpy (arr[0], "The test of";
    	strcpy (arr[1], "There is a house in New Orleans city");
    	strcpy (arr[2], "All good things must come to an end eventually.");
    	strcpy (arr[3], "Formula1 is currently on tele so it is.");
    
    	for (i=0; i<NUM_LINES; i++) //outer loop (each sentence)
    	{
    		len = strlen(arr[i]);
    		
    		for (j=0; j<len; j++)  //inne rloop (each char per sentence)
    		{
    			while (isalpha(arr[i][j])) //are we reading a word
    			{
    				if (start_pos == -1)  //set start position
    					start_pos = j;
    
    				j++;
    				cnt++; //count the length of the word
    			}
    
    			//just read a word
    			end_pos = start_pos + cnt; //set end position
    
    			if (cnt == 4) //was it a 4 letter word ?
    				strcat(temp[i], "****");
    			else
    				strncat(temp[i], arr[i][start_pos], cnt);				
    
    			cnt=0;
    			start_pos=-1;
    			end_pos = 0;
    		}
    		temp[i][100]='\0';		
    	}
    }
    


  • Advertisement
  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    KStaford wrote: »
           // .....
            for (j=0; j<len; j++)  //inne rloop (each char per sentence)
            {
                while (isalpha(arr[i][j])) //are we reading a word
                {
                    if (start_pos == -1)  //set start position
                        start_pos = j;
    
                    j++;
                    cnt++; //count the length of the word
                }
         // .....
    
    Why are you using that while loop?
    // This is only pseudo/c code 
    // Inner loop ...
    for (int j = 0; j < len; ) {
        while (word_sep) { j++; }
    
        // Theres possibly a word next so read till next space or end of string
        // word_sep = [" ", "\t"]
        word_len = 0;
        while (arr[j] != word_sep && j++ < len) {
           ++word_len;
        }
    }
    


  • Closed Accounts Posts: 815 ✭✭✭KStaford


    Why are you using that while loop?

    I need two for loops, 1 to loop through each sentence, another to loop thru each character in a sentence.

    Im using the while loop to determine if i am currently at a word.
    while (isalpha(arr[j]))

    While the current char is an alphabetic character,
    set the start pos
    count each letter until we rae no longer on a word
    set the end position

    If I run the code step by step in debug mode, it runs pretty well, (just need to include spaces now). But the program eventually crashes.

    I dont understand your psuedo code Tobias. Can anyone shed light on this or have a look at my code in Visual Studio ?


  • Registered Users Posts: 9 mowgli


    After a few pints on a Sat night there's something wrong when I feel the need to write c code :)

    Anyway.....
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>


    #define NUM_LINES 4


    int main ()
    {

    char arr[ NUM_LINES ][ 100 ]; //2d array to store multi line text
    int i, j, k; //for loop controls
    int cnt = 0, start_pos = -1, end_pos = 0;
    int len = 0;

    strcpy( arr[ 0 ], "The test of\n" );
    strcpy( arr[ 1 ], "There is a house in New Orleans city\n" );
    strcpy( arr[ 2 ], "All good things must come to an end eventually.\n" );
    strcpy( arr[ 3 ], "Formula1 is currently on tele so it is.\n" );

    for( i = 0; i < NUM_LINES; i++ ) //outer loop (each sentence)
    {
    len = strlen( arr[ i ] );

    for( j = 0; j < len; j++ ) //inner loop (each char per sentence)
    {
    if( start_pos == -1)
    {
    start_pos = j;
    }

    while( isalpha( arr[ i ][ j ] ) ) //are we reading a word
    {
    j++;
    cnt++; //count the length of the word
    }
    //just read a word

    if( cnt == 4 ) //was it a 4 letter word ?
    {
    for( k = 0; k < 4; k++ )
    {
    arr[ i ][ j - ( k + 1 ) ] = '*';
    }
    }

    cnt = 0;
    start_pos = -1;
    end_pos = 0;
    }
    printf( "%s", arr[ i ] );
    }

    return 0;
    }

    produces:
    The **** of
    There is a house in New Orleans ****
    All **** things **** **** to an end eventually.
    Formula1 is currently on **** so it is.

    Is this what you're looking for?


  • Closed Accounts Posts: 815 ✭✭✭KStaford


    mowgli

    Thanks for that. But I think the lecturer wants us to use string functions (eg strcat). The latest lecture was on strings, see my post above about this.

    any ideas (and thanks again, I really appreciate the effort)?


  • Registered Users Posts: 9 mowgli


    just off to bed when I saw your post....
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>


    #define NUM_LINES 4

    int main ()
    {

    char arr[ NUM_LINES ][ 100 ]; //2d array to store multi line text
    int i, j, k; //for loop controls
    int cnt = 0, start_pos = -1, end_pos = 0;
    int len = 0;
    char *replacement = "****";

    strcpy( arr[ 0 ], "The test of\n" );
    strcpy( arr[ 1 ], "There is a house in New Orleans city\n" );
    strcpy( arr[ 2 ], "All good things must come to an end eventually.\n" );
    strcpy( arr[ 3 ], "Formula1 is currently on tele so it is.\n" );

    for( i = 0; i < NUM_LINES; i++ ) //outer loop (each sentence)
    {
    len = strlen( arr[ i ] );

    for( j = 0; j < len; j++ ) //inner loop (each char per sentence)
    {
    if( start_pos == -1)
    {
    start_pos = j;
    }

    while( isalpha( arr[ i ][ j ] ) ) //are we reading a word
    {
    j++;
    cnt++; //count the length of the word
    }
    //just read a word

    if( cnt == 4 ) //was it a 4 letter word ?
    {
    strncpy( &arr[ i ][ j - 4 ], replacement, 4 );
    }

    cnt = 0;
    start_pos = -1;
    end_pos = 0;
    }
    printf( "%s", arr[ i ] );
    }

    return 0;
    }

    sorry, it's too late for anything better..........


  • Advertisement
  • Closed Accounts Posts: 815 ✭✭✭KStaford


    thats brilliant mate. My hat off to you

    Why do we need to use the address of the array in strncpy as in strncpy(&arr[j-4] . . . .?
    Why not just strncpy(arr[j-4] . . . ?

    Thank u again mate


  • Registered Users Posts: 2,013 ✭✭✭SirLemonhead


    This page explains it pretty well.

    http://pw1.netcom.com/~tjensen/ptr/ch6x.htm


Advertisement