0

I have one more question, I want to add a _ in front of every Capital letter which will be converted to lowercase, plus the first letter cannot be capital!! I cant figure out how to do it... :{ example:

input: loLollL, output: lo_loll_l and I want it to go backwards too: input: lo_loll_l output: loLollL

code is here:

#include <iostream>
#include <algorithm>

using namespace std;

int main ()
{
    const int max = 100;
    string slovo;
    int pocet_r;

    cout << "Zadaj pocet uloh:" << endl;
    cin >> pocet_r;

    if(pocet_r >= 1 && pocet_r <=100)
 {

     // funkcia na zabezpecenie minimalneho poctu chars
          for (int i = 0; i <pocet_r; i++)
     {
           cout << "Uloha " << i+1 << ":" << endl; 

                cin >> slovo;

                if(slovo.size() > max)
                {
                 cout << "slovo musi mat minimalne 1 a maximalne 100 znakov" << endl;
                }
                 while( slovo.size() > max) 
                 {
                  cin >> slovo;
                 }      

                 for (int i=0; i <= slovo.size(); i++)
                 {
                   int s = slovo[i];
                   while (s > 'A' && s <= 'Z')
                   {
                      if(s<='Z' && s>='A'){
                      return s-('Z'-'_z');
                      }else{

                      cout <<  "chyba";

                      }
                   } 


                }


           cout << slovo[i] << endl;   

     }   

 }else{
     cout << "Minimalne 1 a maximalne 100 uloh" << endl;
}
system("pause");
}

EDIT>

for (int i=0; i <= slovo.size(); i++)
            {
                while (slovo[i] >= 'A' && slovo[i] <= 'Z')
                {
                      string s = transform(slovo[i]);

    cout << s << endl;

    s = untransform(s);

    cout << s << endl;
}
                      }
frank17
  • 107
  • 12
  • 1. The backwards result is not uniquely defined, even if `_` is forbidden in the original string, `lo_loll_l` could trnsform to `loLollL` or `LoLollL`. 2. `'_z'` is not a character, is this a typo? –  Feb 02 '14 at 15:30
  • 3. Why do you return from `main` in the middle of you code? This will end the program. 4. I do not understand variable names and output text. This would be easier if you translated all of them to English. 5. At some point you should assign to the new string, you never do that. –  Feb 02 '14 at 15:37
  • I updated what my code should do, the string cant start with capital letter – frank17 Feb 02 '14 at 15:39
  • Im learning C++ so anything whats wrong, just tell me, show me how it should look like, please..im dealing with it for hours :( – frank17 Feb 02 '14 at 15:40
  • Since you didn't say what's wrong with your code, I'm voting to close as a duplicate of [Wrong answer for code to convert between Java camel case and C++ underscore identifiers](http://stackoverflow.com/questions/21356847/wrong-answer-for-code-to-convert-between-java-camel-case-and-c-underscore-iden), which should lead you to a correct answer. – Bernhard Barker Feb 02 '14 at 15:45

1 Answers1

0

This should work:

#include <string>
#include <cctype>
#include <iostream>

using namespace std;

string
transform(const string& s)
{
    const size_t n = s.size();
    string t;

    for (size_t i = 0; i < n; ++i)
    {
        const char c = s[i];

        if (isupper(c))
        {
            t.push_back('_');
        }

        t.push_back(tolower(c));
    }

    return t;
}

string
untransform(const string& s)
{
    string t;

    const size_t n = s.size();
    size_t i = 0;

    while (i < n)
    {
        char c = s[i++];

        if (c != '_')
        {
            t.push_back(c);
            continue;
        }

        c = s[i++];

        t.push_back(toupper(c));
    }

    return t;
}

int
main()
{
    string s = transform("loLollL");

    cout << s << endl;

    s = untransform(s);

    cout << s << endl;
}
user3146587
  • 4,250
  • 1
  • 16
  • 25
  • thanks, so now I have to put it in heading of my code and then the rest into main function? – frank17 Feb 02 '14 at 15:46
  • @feri You can take the code for `transform` and `untransform` as is (+ the additional `#include` directives). Then adapt your `main` function to call these functions to apply the transformation to strings your program is reading from `cin`. – user3146587 Feb 02 '14 at 15:48
  • I updated my code, but im getting error when calling transform – frank17 Feb 02 '14 at 15:54
  • @feri `transform` has to be called on the whole string `slovo` not a single character. – user3146587 Feb 02 '14 at 15:56
  • yeah I got it now, but what it does is that it shows it backwards right away, and I dont want it, I want to show it backwards when user types the input backwards, so if I type: lolKO, my output is: lol_k_o, but when I type lol_k_o, then I get lolKO output, not right away,do u understand me? thanks a lot man! – frank17 Feb 02 '14 at 16:01
  • @feri Then check whether the input string `slovo` contains a `_` character. And depending on this, call either `transform` or `untransform` on `slove` to apply the forward or inverse transformation (note: this is not the initial question anymore). – user3146587 Feb 02 '14 at 16:04
  • thanks, finding the characted is working now, but i tried to type lol_lol and i got output starting with capital and it can start only with lowercase – frank17 Feb 02 '14 at 16:55
  • @feri The answer was edited some time ago to follow the changing requirements for the first letter in your question. The latest version should not make the first letter uppercase in `untransform`. – user3146587 Feb 02 '14 at 17:09