How To Find String Length

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


we cannot builtin functions to find string length
Then you probably will and should use the same convention as C-language uses for "strings"; a null-terminated array of characters.
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.
int FindLengthOfString (char TheArray[] , int SizeOfArray)
{
for (int x=0; x!='\0'; x++);
return x;


}


int main()
{
char name [1000];

cin.getline(name,1000);

FindLengthOfString(name , 1000);

}



// Can You People please tell the error in above program
Return exits the function and produces the return value.

I think you want to increment instead.
Last edited on
@ 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.
#include <iostream>
using namespace std;

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

using namespace std;






int FL(char arr[], int SizeOfString)
{
    for (int x=0; arr[x]!='\0'; x++)
    {
    return (x);
    }
}



int main ()
{
        const int SizeOfString = 1000;
        char name [1000];

        cin.getline(name,1000);

        FL(name , 1000);
        cout << FL;
}



This Fuction always return 1 ??
Last edited on
This Fuction always return 1 ?

The return statement should be outside the loop.
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.
@ Chervil Sir When I Put it outside the loop

It gives the following error

name lookup of 'x' changed for ISO 'for' scoping
1
2
3
4
for (int x=0; arr[x]!='\0'; x++)
{
return (x);
}


x only exists between {} in your for loop

1
2
3
4
5
{
	int x;
	for (x = 0; arr[x] != '\0'; x++){}
	return x;
}


x now exists between the outer {}

http://msdn.microsoft.com/EN-US/LIBRARY/b7kfh662(v=vs.80).ASPX
Topic archived. No new replies allowed.