.....
Printable View
.....
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?
As in:
That limits the input to MAX-1 characters, thus preventing a buffer overflow.Code:scanf("%19s", word);
so are you saying to do something like
in stringlen
*pWord=null
while
*pWord != '\0'
return;
i am doing this for class and am not allowed to change my main
That is the C convention for character strings ie they are null terminated so that stdlib routines like strlen() etc. can find its end. In the case of stringLen() too start at the beginning and keep incrementing pWord until a null character '\0' is reached. In pseudocode it'll be something like
Code:pWord points to the beginning of the array
set character count equal to zero
while (null character isn't found)
increment pWord
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.
umm what i wrote was for stringLen
is that not correct either?
i can not add or modify main at all.
so is the what you mean itCbitC
//code
stringLen(char *pWord)
{
int count
count=0;
while
++pWord;
++count;
return;
>> so is the what you mean itCbitC
Why don't you compile it and see for yourself?
i compiled it but it gave me token error.
when it says token error whats it mean?
>> when it says token error whats it mean?
It means the compiler encountered an 'unexpected' character sequence. The easiest way to go about this is to strip everything down until you have something that compiles. First write a function that does nothing. Return a default value. If that compiles, try to set up an empty loop. If that compiles, add a bit of code little by little, compiling and printing values after every change. That's the basic idea, anyway. And above all, find good books, or online tutorials, and read them over and over until you actually understand the syntax, before actually attempting to write code.