• 14
• 12
• 9
• 10
• 13

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

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

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 on other sites
Quote:
 Original post by RauniThis is what i'm doing:#include #include #include 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 on other sites
Quote:
 Original post by RauniThis is what i'm doing:#include #include #include 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 on other sites
Quote:
 Original post by Raunicout << 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 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 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';
}