write a program to find the length of the string without using control structures and without using string.h header files???
program to find the length of the string manually
Collapse
X
-
Originally posted by goelvivekwrite a program to find the length of the string without using control structures and without using string.h header files???Comment
-
Originally posted by weaknessforcatsA C string is a char array with a \0 as the last character. Just count the characters in the array without counting the \0.
C++ string
approximate:
Code:cpp class string { private: struct ChatT { ... ... blablabla; }; ChatT *s; int __length; //<---- private mamber of class string public: int length() { return this->__length; } } ;
Comment
-
If you are using character arrays e.g. char string[256], Then all you have to do is to read each element of the string from the start 0 or 1 until char string[x] = "\0".
You can do this in any loop.
For example, no error checking done
Code:char string[256]; int x; cin>> string; // or physically assign each element e.g. string[1] = a and string[2] =b while (string(x) <> "\0") { x++; } cout<< (x-1); // this will give the last value of the string before string(x) = "\0"
Comment
-
Thre previous 9 posts are riddled with errors:
Originally:
Originally posted by goelvivekwrite a program to find the length of the string without using control structures and without using string.h header files???
Originally posted by PuzzleCwhat is a type C string?
C++ string
approximate:
Code: ( text )
cpp
class string
{
private:
struct ChatT
{
...
...
blablabla;
};
ChatT *s;
int __length; //<---- private mamber of class string
public:
int length() { return this->__length; }
} ;
A C string is an array of char with the last element a binary 0 ('\0').
Originally posted by Studlyamiwhen you say the length of the string, does string refer to a variable of string type or is this a character array?
Originally posted by chazzy69If you are using character arrays e.g. char string[256], Then all you have to do is to read each element of the string from the start 0 or 1 until char string[x] = "\0".
You can do this in any loop.
For example, no error checking done
Code: ( text )
char string[256];
int x;
cin>> string; // or physically assign each element e.g. string[1] = a and string[2] =b
while (string(x) <> "\0") {
x++;
}
cout<< (x-1); // this will give the last value of the string before string(x) = "\0"
1) cin >> string will only fetch a string that has no whitespace in it. Usually, this means you fetch only one word of the input.
2) string[x] = "\0" won't compile. string[x] is a char. "\0" is an empty string. That is, an array with \0 as element[0]. In C and C++ the array name is the address of element[0] so this compares a char to the address of the char \0. The compiler will gag on this.
3) while (string(x) <> "\0") { won't compile. string(x) is a function call to the function string using the argument x. Proably you meant string[x]. However the <> won't work, again that compares a char to the addess of a char. See (2) above.
Now read my Post #5 again and follow the direction exactly.Comment
Comment