LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2005, 07:11 AM   #1
sajith
Member
 
Registered: Sep 2005
Location: kannur
Posts: 59

Rep: Reputation: 15
strlen() function problem in C++


hai sir

i have problem when using the strlen() function in C++

the code of that is

#include <iostream>
#include <cstring>
using namespace std;

int main() {
char str1 = 'b';
string str2 = "foo";
string str3;
int l=strlen(str2); //error
cout<<l<<"\n";
str3 = str2 + str1;
cout << str3 << endl;
return 0;
}


error i got is

:9: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’


please tell me solution
 
Old 09-28-2005, 07:53 AM   #2
sind
Member
 
Registered: Jun 2005
Posts: 75

Rep: Reputation: 15
It looks like jtshaw has answered your question here. jtshaw's comments about strcat() apply to strlen() as well.

The length() member function of std::string will do what you're after.

~sind
 
Old 09-28-2005, 08:20 AM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Here is a list of things the STL String (aka C++ String) class contains.
 
Old 01-14-2006, 06:34 AM   #4
stephen9
LQ Newbie
 
Registered: Jan 2006
Posts: 1

Rep: Reputation: 0
hey guys can anyone help me how to use string.h to create a singular/plural program, is a project of mine and i need it to get A+ on it erm can anybody help me?????
 
Old 01-14-2006, 03:48 PM   #5
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by stephen9
hey guys can anyone help me how to use string.h to create a singular/plural program, is a project of mine and i need it to get A+ on it erm can anybody help me?????
This is unrelated to this thread; start a new thread. Also, we don't help people with homework.
 
Old 01-14-2006, 03:53 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by spooon
... Also, we don't help people with homework.
Why do people say this?
The rules say
Quote:
Do not expect LQ members to do your homework - you will learn much more by doing it yourself.
There is helping someone and then theres doing the work. These two are completely different. Post your question stephen9 and I will try and help.

Last edited by dmail; 01-14-2006 at 03:55 PM.
 
Old 01-16-2006, 10:21 AM   #7
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
fixed code

#include <iostream>
#include <cstring>
using namespace std;

int main() {
char str1 = 'b';
string str2 = "foo";
string str3;
//int l=strlen(str2); //error
int l=strlen("str2");
cout<<l<<"\n";
str3 = str2 + str1;
cout << str3 << endl;
system("pause");
return 0;
}
 
Old 01-16-2006, 02:53 PM   #8
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by kike_coello
//int l=strlen(str2); //error
int l=strlen("str2");
No, this is very wrong. You are taking the length of the literal string "str2", which is always 4; and you are not doing anything with the variable str2 itself, which is the intended action. Correct way would be
Code:
int l = str2.length();
 
Old 01-16-2006, 08:44 PM   #9
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
hey dude, how is it wrong, when it worked, i ran that code on xp.
 
Old 01-16-2006, 09:14 PM   #10
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
As typed "int l=strlen("str2");" takes the length of the string "str2" not the string held in the variable str2. str2.length() is correct.

Here is an example and it's output:

Code:
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
        string str2="foo";
        int len1 = str2.length();
        int len2 = strlen("str2");
        int len3 = strlen(str2.c_str());

        cout << "str2.length() = " << str2.length() << endl
             << "strlen(\"str2\") = " << strlen("str2") << endl
             << "strlen(str2.c_str()) = " << strlen(str2.c_str()) << endl;
}
Code:
johnshaw@Quaqmire-OSX ~ $ ./test2 
str2.length() = 3 
strlen("str2") = 4
strlen(str2.c_str()) = 3
I can not think of any situation where you want to put a variable in quotes in the C++ language. If you want to use the strlen function (which is a C function) you need to use the .c_str() method on your stl string.

Last edited by jtshaw; 01-16-2006 at 09:41 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strlen problem salahuddin_66 Programming 2 11-13-2005 04:21 AM
C++ question: strlen error!? Hady Programming 2 03-16-2005 05:08 AM
confused about strlen() mcd Programming 19 02-26-2005 08:04 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration