Jump to content
  • Advertisement
Sign in to follow this  
Rauni

Questions about DevC++.. strlen() and converting to uppercase!

This topic is 5039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is what i'm doing: #include <string> #include <iostream> #include <stdlib.h> using namespace std; int length=0; string string1="" int main() { cin >> string1; cout << length=strlen(string1); system("PAUSE") return 0; } but it returns the following error: 26 C:\DevC Projects\Project1\main.cpp cannot convert `std::string' to `const char*' for argument `1' to `size_t strlen(const char*)' And second question: How to implement (and call) the function that would convert the string string1 to uppercase? thx in advance for repliers..

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Rauni
This is what i'm doing:

#include <string>
#include <iostream>
#include <stdlib.h>
using namespace std;

int length=0;

string string1=""

int main()
{
cin >> string1;
cout << length=strlen(string1);
system("PAUSE")
return 0;
}

but it returns the following error: 26 C:\DevC Projects\Project1\main.cpp cannot convert `std::string' to `const char*' for argument `1' to `size_t strlen(const char*)'

Of course, it requires a char* variable.
Just use the c_str() function:
cout << length=strlen(string1.c_str());
Quote:

And second question: How to implement (and call) the function that would convert the string string1 to uppercase?

thx in advance for repliers..


Check whether the char is lower case a-z (96-121) and then subtract 31 from it to get upper case A-Z (65-90):
void UpperCase(string s)
{
for (int i = 0; i < s.size(); i++)
if (s > 95 && s < 122)
s = s - 31;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Rauni
This is what i'm doing:

#include <string>
#include <iostream>
#include <stdlib.h>
using namespace std;

int length=0;

string string1=""

int main()
{
cin >> string1;
cout << length=strlen(string1);
system("PAUSE")
return 0;
}

but it returns the following error: 26 C:\DevC Projects\Project1\main.cpp cannot convert `std::string' to `const char*' for argument `1' to `size_t strlen(const char*)'

And second question: How to implement (and call) the function that would convert the string string1 to uppercase?

thx in advance for repliers..

The string class has a string length method built in, you would call it as string1.length().

toupper:

#include <algorithm>
#include <string>

using namespace std;

string some_string = "hello";
transform (some_string.begin(),some_string.end(), some_string.begin(), toupper);




or you could write your own

string toupper(const string $str){
string temp=str;
for(int i=0;i<temp.length();i++)
temp+=(temp>='a' && temp<='z')?('A'-'a'):0;
return temp;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Rauni
cout << length=strlen(string1);

I believe that line is evaluated like this:

( cout << length ) = strlen(string1);

Is that what you intended?

I generally don't mix assignment in with other operations because it doesn't accomplish anything and it makes the code harder to understand. Is it better than this?

length = strlen(string1);
cout << length;

Share this post


Link to post
Share on other sites
For the length, this would be better (and faster):
cout << length=string1.length();
And you don't even need the length= bit if you're not going to use the variable.

As for converting to upper case I do this:
strupr(&string1[0]);
I.e. bastardise the C function :P

Share this post


Link to post
Share on other sites
Another way is to use the transform() algorithm with the toupper() function, for example:

#include <algorithm>
// ...

int main() {
string s("The quick brown fox.");
cout << s << '\n';
transform(s.begin(), s.end(), s.begin(), toupper);
cout << s << '\n';
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!