printing a string via a function/pointer/array/i don't know what i'm talking about

This is a discussion on printing a string via a function/pointer/array/i don't know what i'm talking about within the C Programming forums, part of the General Programming Boards category; ok so the assignment is for the user to enter some ........ and then the program prints it out, then ...

  1. #1
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34

    printing a string via a function/pointer/array/i don't know what i'm talking about

    ok so the assignment is for the user to enter some ........ and then the program prints it out, then pritns the number of characters, words, lines, etc. I'm trying to modify the first function 'getstring' so it sends the string back into main to get printed. My first question is at the end what does this mean:
    Code:
    string[count] = '\0';
    ? well what does '\0' mean? my second question is how can i modify that function to print int he main? changing the main function isn't allowed.

    Code:
     #include <stdio.h>
    
    #define SIZE 200
    
    void getString( char* );
    void parseString( char*, int*, int*, int* );
    int isWhiteSpace( char );
    
    int main()
    {
        char string[SIZE]; // The string that stores the users input
        int lines = 0;     // The total number of lines the user inputs
        int words = 0;     // The total number of words the user inputs
        int chars = 0;     // The total number of chars the user inputs
    
        // Get input from the user and put it in "string"
        getString( string );
        // Parse the string and count the # of lines, words and characters
        parseString( string, &lines, &words, &chars );
    
        // Print the text that the user entered
        printf( "\n\nHere is the text you entered:\n" );
        printf( "%s", string );
    
        // Print the number of lines, words and characters
        printf( "\n\nLines: %d\n", lines );
        printf( "Words: %d\n", words );
        printf( "Chars: %d\n", chars );
    
        return 0;
    }
    
    /**
     * This function gets input from the user
     * and stores it in the string which is
     * passed into the function.
     */
    void getString( char *string )
    {
        char c;
        int count = 0;
    
        while( (c = getchar()) != '^' && count < SIZE-1)
        {
            string[count++] = c;
        }
        string[count] = '\0';
    }
    
    /**
     * This function iterates through "string"
     * and counts the number of lines, words and
     * chars. The totals are stored at the locations
     * that are referred to by the three pointers
     * to integers.
     */
    void parseString( char *string, int *lines_ptr, int *words_ptr, int *chars_ptr )
    {
        /**
         * Suggestion: Use two pointers to iterate through
         * the string and find where words begin and calculate
         * all the totals.
         *
        char *prev; // Previous location in string
        char *curr; // Current location in string
        */
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by DLR
    My first question is at the end what does this mean:
    Code:
    string[count] = '\0';
    ? well what does '\0' mean? my second question is how can i modify that function to print int he main? changing the main function isn't allowed.
    First, '\0' is the null termination of the string. Second, it looks like a '^' breaks the loop.
    Code:
    hello world^
    
    
    Here is the text you entered:
    hello world
    
    Lines: 0
    Words: 0
    Chars: 0
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    Quote Originally Posted by Dave_Sinkula
    First, '\0' is the null termination of the string. Second, it looks like a '^' breaks the loop.
    Code:
    hello world^
    
    
    Here is the text you entered:
    hello world
    
    Lines: 0
    Words: 0
    Chars: 0
    oh duh ^
    that's why nothign was working

  4. #4
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    ok now i'm trying to figure out how to make it count the number of characters

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You've already got a great example in the function getString.

    [edit]But I suppose the syntax might be a bit confusing as passed to your parse routine.
    Code:
       for ( ; *string; ++string )
       {
          *chars_ptr += 1;
       }
    [edit=2]Or
    Code:
       int i;
       for ( i = 0; string[i] != '\0'; ++i )
       {
          *chars_ptr += 1;
       }
    Last edited by Dave_Sinkula; 03-30-2006 at 09:34 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    thanks dave_sinkula once i somehow get started i get the hang of it
    we'll see how it goes from here

  7. #7
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    here's what i got for counting lines

    Code:
      for(j=0; string[j] !='\0'; ++j)
       {
    	   if (string[j] == '\n')
    	   
    	   { 
    		   *lines_ptr +=2;
    	   }
    now i'm trying to figure otu how to use
    Code:
    isspace()
    to count words. I was going to do something along the lines of have isspace count the number of spaces, then add 1 to that number to get the number of words, but that wouldn't work if there were more than 1 space in a row.

  8. #8
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    ok well this is what i ahve so far
    Code:
    void parseString( char *string, int *lines_ptr, int *words_ptr, int *chars_ptr )
    {
    	
    	 int i;
    	 int j;
    	 int k;
    
       for (i=0; string[i] !='\0'; ++i )
       {
          *chars_ptr += 1;
       }
    
       for(j=0; string[j] !='\0'; j++)
       {
    	   if (string[j] == '\n')
    	   
    	   { 
    		   *lines_ptr +=2;
    	   }
    	   
       }
    
    		for (k=0; string[k] !='\0'; ++k)
    	{
    		if (string[k] == ' ')
    		{
    			*words_ptr +=2;
    		}
    
    		}
    
    
    
        
    }
    if you run it you will see that it has a few problems. I can't figure out how to fix them though.
    problem 1: if there is only 1 line of characters it will say 0, otherwise it seems to work fine
    problem 2: if there's more than 1 space between words it counts taht as a word.


  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Before you get too much further along, I'd say use the recommendation.
    Code:
    void parseString( char *string, int *lines_ptr, int *words_ptr, int *chars_ptr )
    {
        /**
         * Suggestion: Use two pointers to iterate through
         * the string and find where words begin and calculate
         * all the totals.
         *
         */
       char *prev; // Previous location in string
       char *curr; // Current location in string
    This means changing the loop control a bit.
    Code:
       for ( curr = string, prev = curr; *curr != '\0'; prev = curr, ++curr )
    Now, you can use this same loop once to get all three counter values. For example, since the loop iterates through each character, always bump the character count in the loop. For newlines, check if the current character is a newline to see whether you bump its counter.
    Code:
          *chars_ptr += 1;
          if ( *curr == '\n' )
          {
             *lines_ptr += 1;
          }
    Regarding the use of isspace, just think about what might define a "word". Perhaps if the current character is a space and the previous character was not a space.

    There are little issues given the input routine as well, but I'll leave that for a later round. But I should also note that the function getchar returns an int, so you should really declare c as an int in your function getString.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

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