Sign in to follow this  
Rauni

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

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
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[i] > 95 && s[i] < 122)
s[i] = s[i] - 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[i]+=(temp[i]>='a' && temp[i]<='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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this