Why do you think pWord should be between 1 and 20? What do you think pWord is, exactly?
(Note also that you don't return anything, let alone the length of the string.)
>> This is main and this is my string length whats wrong with it i don't seem to know why it isn't working:
1) It doesn't compile
2) pWord is a pointer, not a length. One way find the length is to traverse the text, and increment a counter until you reach the character value 0.
3) scanf is vunerable to buffer overflows - use a hard-coded width-specifier, or use something like snprintf to imbed the max length into the format string beforehand.
we only have learned scanf and fscanf
In that case walk along the array array pWord until you reach the terminating null character while incrementing the count.
>> we only have learned scanf and fscanf
OK, so use the hard-coded width specifier then, no?
itCbitC i don't understand what you are saying. Why use NULL?
hard coded wdith specifier?
>> itCbitC i don't understand what you are saying. Why use NULL?
Not NULL but null. ;) In C, text is *usually* (but this is just a convention) terminated with the character value 0 (ie: null). Naturally, if you count how many characters precede it, you get the length.
>> hard coded wdith specifier?
That limits the input to MAX-1 characters, thus preventing a buffer overflow.Code:
so are you saying to do something like
*pWord != '\0'
i am doing this for class and am not allowed to change my main
pWord points to the beginning of the array
set character count equal to zero
while (null character isn't found)
increment character count
return character count
>> sp are you saying to do something like
OK, first of all, let's write code that compiles, shall we? Next, why would you set the first character to 0 and then set up a loop that tests that very condition? The null terminator will already be in the buffer. Finally, in the loop you'll need to increment pWord.