i did these function for uppercase:

string Upper(string text)
{
    string result;
    result.resize(text.size());
    for(int i=0; i<text.size(); i++)
    {
        if(text[i]>96 && text[i]<123)
            result[i]= text[i]-32;
        result[i]=text[i];
    }
    return result;
}

but i'm getting the wrong results :(
what i'm doing wrong?

You may want to look into <cctype> which provides facilities for this type of thing. For example:

#include <string>
#include <cctype>

std::string make_upper (const std::string& in) {
    std::string out(in);
    for (::size_t i = 0; i < in.size (); ++i) {
        out[i] = ::toupper(out[i]);
    }
    return out;
}

You can find more about <cctype> here

Comments
thanks

anotherthing: when i do from string to char: i get an error: 'cast from 'const char*' to 'char' loses precision [-fpermissive]|'
what you can advice?

TableMenuShortCuts[TableMenuShortCuts.size()-1].key=(char)j.c_str();

key structure member is char and the j is the string

Edited 1 Year Ago by cambalinho

Something like that:

// removes the third bit, simple way to create upper case char
char toupper(char ch)
{
  return ch & 0xDF;
}

Sorry doesn't handle numbers etc.

Edited 1 Year Ago by sneekula

Comments
thanks

@cambalinho
you are mixing char and int types, do the proper casts and your approach will work:

// str_Upper.cpp
// convert a string to all upper case

#include <iostream>
#include <string>

using namespace std;

string Upper(string text)
{
    string result;

    result.resize(text.size());
    for(int i = 0; i < text.size(); i++)
    {
        if ((int) text[i] > 96 && (int) text[i] < 123)
            result[i] = (char) ((int) text[i] - 32);
        else
            result[i] = text[i];
    }
    return result;
}

int main()
{
  string s = "A test this is 123";

  cout << s << endl;
  cout << Upper(s) << endl;

  return 0;
}

/* output -->
A test this is 123
A TEST THIS IS 123
*/
Comments
thanks

@sneekula: why use an approach that forces limits on the input? toupper is designed specifically for this purpose and has a corresponding towupper for wide characters. Byte ranges and bit masking only limit functionality.

Comments
thanks

@cambalinho The reason you are gettting that error is std::string.c_str() returns a char* to the c-string the string contains. A char and a char* are not the same thing. If you want just the first character from the c-string then you can use:

TableMenuShortCuts[TableMenuShortCuts.size()-1].key=(j.c_str())[0];

// or

TableMenuShortCuts[TableMenuShortCuts.size()-1].key=*(j.c_str());
Comments
thanks

@L7Sqr
In all fairness SneeKula answered the original question. Sometimes it is fun to experiment a little to get to know the language better. Yes, I would rather use the tested tolower() and toupper() functions.

Some instructors give these kind of problems to their students to make them think.

Comments
Agreed. I sometimes forget about the fun of exploration as a result of having been burnt in the past.
thanks

i'm sorry to both, but i continue with problems :(

string Ustrcaption=UpperCase(strCaption);
        String b=Ustrcaption;//is like the normal string, but have the separewordssymbols() and more functions
        vector<string> c;
        c=b.separewordssymbols();//tested and the results are fine

        TableMenuShortCuts.resize(TableMenuShortCuts.size()+1);
        TableMenuShortCuts[TableMenuShortCuts.size()-1].MenuPointer=(ULONG_PTR)this;
        TableMenuShortCuts[TableMenuShortCuts.size()-1].key=(c)[c.size()-1][0];
        if(strCaption=="&Exit\tCtrl+g")
            //SetWindowText(MainHWND,(LPCSTR)TableMenuShortCuts[TableMenuShortCuts.size()-1].key);//empty string or even don't change it
            SetWindowText(MainHWND,(LPCSTR)c[c.size()-1].c_str());//the char is showed wihtout problems

please read the comments.
the TableMenuShortCuts[TableMenuShortCuts.size()-1].key don't recive the results correctly :(
@NathanOliver: i'm testing what you said too :(

heres the 2 functions rebuilded:

string UpperCase (const string& in)
{
    string out(in);
    for (size_t i = 0; i < in.size (); ++i)
    {
        out[i] = toupper(out[i]);
    }
    return out;
}

string LowCase (const string& in)
{
    string out(in);
    for (size_t i = 0; i < in.size (); ++i)
    {
        out[i] = tolower(out[i]);
    }
    return out;
}

thanks to all

You can modernize this a little:

// str_toupper.cpp
// convert a string to all upper case
// compiled with mingw32-g++.exe

#include <algorithm>  // transform()
#include <cctype>     // toupper()
#include <iostream>
#include <string>

using namespace std;

char upper_case(char ch)
{
  return toupper(ch);
}

int main()
{
  string s = "this is a Test 123";

  transform( s.begin(), s.end(), s.begin(), upper_case );
  cout << s << endl;

  return 0;
}

/* result:
THIS IS A TEST 123
*/
This question has already been answered. Start a new discussion instead.