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

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 ...

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.

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.

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.

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.