Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Determining length of a string without using strlen
#1
In my CS130 class, one assignment requires me to print out 8 business cards, 4 rows high and 2 columns high. Easy. The only slight problem was that, to draw the 2 columns of business cards, I do something like this:

cout << strCompanyname << strWhitespace << strCompanyname << endl;

This was done for every line of the card, and as a result the card looks something like this:

Quote:Black_toque_software Black_software
Regina,_sk Regina,_sk
blah blah blah blah blah blah
sdkfsod sdkfsod

Of course, this is because strWhitespace is the same value. So, to fix this I decide to make another function that returns a string of whitespaces just the right length so the second column would look nice and even. One of the things I had to do was find the length of the string. "No problem", I think, "Just use the strlen." However, I got a rather nasty surprise when I got this:

Quote:Error: Could not find a match for std:Confusedtrlen(std::basic_string<char, std::char_traits<std>, std::allocator<std>>).

googling found nothing for this error. So, I've been wondering, does my compiler support strlen?

Btw, Although i'm running windows, I'm SSHing to the school servers and running the unix terminal. The compiler i'm using is CC... that's all I know about it. Help would be appreciated about how to get the strlen working or about an alternative to it.

Oh, one last thing... for strings, instead of using character arrays, we're declaring our strings like this:

string stringname;

where stringname contains a string of characters, but will not hold spaces.
Jumping Jahoolipers!
Reply
#2
There are 2 alternatives-

1) Use the c_str() member function to convert the string to a C-style string (char array of type "const char*") for use in the strlen() function:
Code:
cout << strlen(stringname.c_str()) << endl;
2) Use the length() member function to return the length, without dealing with functions inherited from the C programming language:
Code:
cout << stringname.length() << endl;
Both should return the same result, if I remember correctly.

Edit: Method 2 is pure C++ stuff, whereas method 1 mixes C and C++ .
974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Reply
#3
Yeah, I tried the length function, but I wasn't sure how it worked... so just let me get it straight...

it goes like this?

Code:
string stringname;

cout << stringname.length();

I was trying to use it like this...

Code:
string stringname;

cout << length(stringname);
Tongue I blame FB. Wink
Jumping Jahoolipers!
Reply
#4
You have a right to blame FB. :-P

I'm learning C# little-by-little. In C#, it is stringname.Length (note the capital 'L' and lack of parentheses). I blame C++ for thinking it was stringname.length(), and BASIC for thinking it was stringname.len(). ;-)
974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Reply
#5
Quote:1) Use the c_str() member function to convert the string to a C-style string (char array of type "const char*") for use in the strlen() function:
Code:
cout << strlen(stringname.c_str()) << endl;

Don't use this one. It'll badly fail if the string contains NULs.

Quote:2) Use the length() member function to return the length, without dealing with functions inherited from the C programming language:
Code:
cout << stringname.length() << endl;

I personally prefer stringname.size() because size() is the common class member that returns the size of a container. IOW: object.size() will work with every container like vector, list, map, string, etc...

Regards,
Mark
Reply
#6
there is a slightly simpler approach....

use "\t"....should align columns.....i think it does it in chunks of 8 characters......so < 8 chars from a newline will be in tab 1, >8 and <16 is tab 2, etc.

i think it's 8...if not, then it's 10

just check string lengths so you get the right column every time

oz~
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)