-4

Hi guys I've run into a problem,for some reason a blank string is being printed or you could also say nothing is being printed when I try to print out the string,this only occurs when I include a capital letter in the string such as acB if I type acb it sorts and prints them with no problems,I added a continue statement in to the for loop because I thought this would skip the rest of the code and go to the next iteration if that block of code got executed but to no avail anyway here is my code.

void order(char *str,int size){


     bool sorted = false;

     while(!sorted){

        sorted = true;

        for(int i = 0; i < size-1; i++){

              if(str[i] >= 'A' && str[i] <= 'Z'){

                if((str[i+1])-32 < str[i]){

                    char temp2 = str[i];
                    str[i] = str[i+1];
                    str[i+1] = temp2;
                    sorted = false;
                    continue;
                }
              }
              if(str[i+1] < str[i]){

                char temp = str[i];
                str[i] = str[i+1];
                str[i+1] = temp;

                sorted = false;
            }
        }
     }
}
int main()
{
    char str[] = "aCb";
    int size = sizeof(str) / sizeof(char);
    order(str,size-1);
    cout << str << endl;
}
Iosif Murariu
  • 2,019
  • 1
  • 21
  • 27
irishmaniac
  • 101
  • 1
  • 12
  • 10
    Put a breakpoint at `continue` and debug your code – Asesh Dec 05 '17 at 15:31
  • You are sending size-1 and looping again till size-1 means that you are always skipping last 2 elements of the array. – Asim Dec 05 '17 at 15:34
  • You check if the letter you are currently looking at is a capital letter but you don't check if the letter you are comparing it to is a capital... – Liora Haydont Dec 05 '17 at 15:39
  • Run through the code with `"aC"`, in your head. First you will swap the characters (because `'C' < 'a'`). Then you start over with `"Ca"`, which will, again, swap the characters (because `'A' < 'C'`). Then you start over with `"aC"`... – molbdnilo Dec 05 '17 at 15:40
  • @Asim not sure what you mean, – irishmaniac Dec 05 '17 at 16:00
  • @Liora very good point,but before I get to that problem I'm trying to understand why an empty string is being printed – irishmaniac Dec 05 '17 at 16:07
  • If you put a breakpoint you'll be able to see what happends to your str and what is its values when it is printed. – Liora Haydont Dec 05 '17 at 16:35
  • Please [edit] your question to show us what kind of debugging you've done. I expect you to have run your [mcve] within Valgrind or a similar checker, and to have investigated with a debugger such as GDB, for example. Ensure you've enabled a full set of compiler warnings, too. What did the tools tell you, and what information are they missing? And read Eric Lippert's [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Toby Speight Dec 05 '17 at 18:21
  • BTW, `c >= 'A' && c <= 'Z'` is *not* necessarily equivalent to `std::isupper(c)`. – Toby Speight Dec 05 '17 at 18:23

1 Answers1

1
    void order(char *str,int size){


     bool sorted = false;

     while(!sorted){

        sorted = true;

        for(int i = 0; i < size - 1; i++){

          if(str[i+1] >= 'A' && str[i+1] <= 'Z' && str[i] > 'Z'){
            if(str[i+1] < str[i] - 32){
              char temp2 = str[i];
              str[i] = str[i+1];
              str[i+1] = temp2;
              sorted = false;
              continue;
            }
          }
          else{
            if(str[i] >= 'A' && str[i] <= 'Z' && str[i + 1] >= 'Z'){
              if(str[i+1] -32 < str[i]){
                char temp2 = str[i];
                str[i] = str[i+1];
                str[i+1] = temp2;
                sorted = false;
                continue;
              }
            }

            if(str[i+1] < str[i]){
              char temp2 = str[i];
              str[i] = str[i+1];
              str[i+1] = temp2;
              sorted = false;
              continue;
            }
          }
        }
     }
}

int main()
{
    char str[] = "aCB";
    int size = sizeof(str) / sizeof(char);
    order(str,size-1);
    cout << str << endl;
}

First IF: Checks if the first letter is lowercase and the second letter is uppercase.

Second IF: Checks if the first letter is uppercase and the second letter is lowercase.

Third IF: then both letters are uppercase or lowercase.

Davi Leal
  • 11
  • 2
  • hi Davi thanks for the reply much appreciated, but lets say for the first case str[i] will be a capital so if I subtract 32 from it it will not be a alphabetical letter? – irishmaniac Dec 05 '17 at 16:04