function to find the length of a string. Decide the parameters and return type yourself:
____ LengthOfString (___________________)
The caller function should input a string from the user and print its length on the screen by using the above function
For C-style strings, remember that they are an array of characters terminated by a null character. So just count the number of characters up to the null.
A string is basically a null terminated char array. Start at 0 and loop through until you find '\0'.
we cannot builtin functions to find string length
I love homework assignments like these. Do you think they tell your doctor in medical school that he has to diagnose a patient without using any tools simply because "it encourages thinking outside of the box"? Or do you think that your auto mechanic was ever told to change a tire in shop class with his bare hands? Encouraging students to reinvent the wheel and not be aware of the tools at their disposal for the hundreds of already solved problems is probably the exact OPPOSITE of what a university should be doing.
@ rubait: Error number one is that you are trying to hijack a thread instead of creating your own.
Error number two is that you are not using the code brackets supplied by this site. This strips the spacing from your code and makes it that much harder to read.
Error number three is that you are not including any headers or namespaces even though they would be required for the code you have written.
Error number four is that 'main()' should always return an integer. Yours just kind of dies.
Error number five is that you are not catching the value returned from your "FindLengthOfString" function so it is basically being wasted.
Error number six is that you are trying to use the 'x' variable inside of your "FindLengthOfString" function outside of its declared scope.
There maybe more but that's just off the top of my head.
int FindLengthOfString (char TheArray[])
{
for (int x=0; TheArray[x]!='\0'; x++);
return x;
}
int main()
{
char name [1000];
cin.getline(name,1000);
FindLengthOfString(name);
cout<<x;
}
@ Computergeek01
Sir i didn't get you that " trying to hijack a thread instead of creating your own. "
Error Numb 2 : Sir i am new to this site so i dont know how to use these brackets.
Error Numb 3 : Solved
Error Numb 4 : again i didn't get you . i think i wrote int before main
Error Numb 5 : i think i solved it by adding thins line cout<<x;
Error Numb 6 : My compiler is giving the same error but i dont know what actually it is.
1: My mistake, for some reason I didn't think you were the OP.
2: I'm just nit-picking, don't worry too much about this one.
3: So far so good.
4: Declaring what data type a function returns is different from actually returning a value. You should actually have a return statement at the end of "main()". Zero is customary for indicating that there are no errors.
5: You did not. Try cout << FL(name, 1000); and see what happens. You also didn't declare 'x' at first then you tried to use a function as a variable.
6: Better.
(NEW) 7: You are now requiring the user to pass the length of a the string to the function that is supposed to determine the length for them. Try this one again.
(NEW) 8: I didn't notice this before but the char array you have should be a and string data type. Array's aren't necessarily null terminated but strings are.