Am I just imagining something, or is there a strlen command? If there is how do I use it?
I want to be able to measure the length of a string.
Am I just imagining something, or is there a strlen command? If there is how do I use it?
I want to be able to measure the length of a string.
Go you big red fire engine!
yes you can use
using std::strlen;Code:#include <cstring>
for c style strings
or you can do something like
Code:string s; s.length();
I think you're going to needfor it to recognize string in the std namespace.using std::string;
dan
I'm using visual c++ 6.0.
Can I please have a bit more of an explaination, or a small example?
Thankyou.
Go you big red fire engine!
The following was created in Visual C++ as a Win32 console applicaiton. Selecting "Typical Hello world application" then modifying it.
I hope this helpsCode:#include "stdafx.h" //for precompiled headers #include <iostream> //for cout and other c++ methods within iostream class #include <string> //for string class using namespace std; //use entire std namespace avoiding "std::" scope resolution operator int main(int argc, char* argv[]) { string sData = "Test"; cout << "sData contains " << sData << endl; cout << "sData's length is " << sData.length( ) << endl; //or int nStringLength = sData.length( ); cout << "sData contains " << sData << endl; cout << "sData's length is " << nStringLength << endl; return 0; }
hmm. I figured a method out the other day.
#include "iostream.h"
#include "stdio.h" // Used for gets()
int main()
{
char String[80];
cout << "Enter a string ";
gets(String);
int idx = 0;
while(String[idx] != NULL){ // you could replace NULL with 0
idx++;
}
cout << "You typed " << idx << " characters/spaces".
}
I'm not sure if this was the answer you were looking for.
Not Hungarian notation (:
Here is the c style one
Code:#include <cstring> using std::strlen; #include <iostream> using std::cout; using std::endl; int main(void) { char* s = "Hello World!"; int n = strlen(s); cout << "Hello World! is " << n << " charecters long" << endl; return 0; }
forgot to put my name up there.
In the Unregistered post.
gets is dangourous and shouldn't be used.
80 is a pretty good size buffer but someone still
could enter 81 charectors possibly causing a stack overflow.
Using fgets is safer.
#include <iostream.h>
#include <string.h>
int main() {
char string[600];
cout << "Enter a string: ";
cin.getline(string,600);
cout << "Length: " << strlen(string);
return 0;
}
Thankyou for all the help so far, but I still have a problem.
I've been working with dangs code. His code works when it's written like it's shown above, but not when I modify the includes to how they are in my program.
Here are the files that I am including in my program(MS VC++ 6):
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <fstream.h>
#include <ctype.h>
#include <iomanip.h>
When they have the .h on the end dangs code doesn't work, but when i get rid of the .h, hundreds of other errors occur.
I'm not sure why this occurs as I was under the impression that you didn't really need the .h in VC++ anymore (though I have been told to use it - which is why I do). Any help or suggestions will be greatly appreciated, thanks again for all your help so far.
Go you big red fire engine!
You arn't supposed to have .h after std includes
in c++. The reason why you having trouble
is that <string.h> and <string> are different files.
change your includes from :-
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <fstream.h>
#include <ctype.h>
#include <iomanip.h>
to:-
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <cctype>
#include <iomanip>
// and add this line directly underneath the includes...
using namespace std;
also if you are using the STL string you will need to add <string> to that list of includes...
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Thankyou very much - all complete (except for some stupid memory error, but that's not related to this issue).
Go you big red fire engine!