Arranging letters of a word, without upper/lowercase concern

Hi friends,
Today I am asked to make a C++ app which is used to arrange letters of a word alphabetically.
Example:

1.) Input agartala, output aaaagltr (OK)
2.) Input AGARTALA, output AAAAGLTR (OK)
3.) Input agarTala, output aaaaglTr (OK, using pointer)

My question is, how to make the app able to arrange agarTaLa (or other words with jumbled capitalization)?

This is my script so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    char input[100];
    char temp[2],cap_copy[99];
    cin>>input;
    int d=strlen(input);
    for(int j=0;j<d;j++){
    for(int i=d;i>0;i--){
            if(input[j]>=65&&input[j]<=92){
            *cap_copy=input[j];
              }
            if(input[j]>=65&&input[j]<=92){
            input[j]=tolower(input[j]);}
            if(input[i]<input[i-1]){
            *temp=input[i];
            input[i]=input[i-1];
            input[i-1]=*temp;
                            }
            }
    }
    for(int b=0;b<strlen(cap_copy);b++){ 
    for(int s=d;s>0;s--){
                    if(toupper(input[s])==cap_copy[b]){
                    input[s]=cap_copy[b];
    }
    }
}
    for(int a=1;a<=d;a++)
    {cout<<input[a];}
    getch();
}
Last edited on
generally if you wanted to ignore case in a string, you would have to normalize the string first before any compares.

Normalizing usually consists of converting the characters to one form or the other and it doesn't matter which but you want to make things consistent for both sides of the compare for your sort.
Using std::sort:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cctype>
#include <cstring>
#include <algorithm>
#include <iostream>

int main()
{
     char input[100] ; 
     std::cin.getline(input, 100) ;
     unsigned inputSize = std::strlen(input) ;

     struct { 
          bool operator()(char a, char b) { return std::tolower(a) < std::tolower(b); } 
     } cmp;

     std::sort(input, input+inputSize, cmp) ;
     std::cout << input << std::endl ;
}
Last edited on
@cire your code is not working well in Dev. Gets error at line 16 "no matching function for call to `sort(char[100], char*, main()::<anonymous struct>&)' ."

@Azagaros I have tried to use that algorithm. Success for agarTaLa but not Agartala. Typing "Agartala" as input resulted in "AAAAgltr". This is my loop for fulfiling that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
    char input[100],copy[100],capital[100];
    char temp[2];
    cin>>input;
    int d=strlen(input);
    strcpy(copy,input);
....//Bubble sorting the *copy* characters//...
for(int b=0;b<d;b++){
    capital[b]=toupper(copy[b]);
    for(int s=0;s<d;s++){
            if(capital[b]==input[s]){
            copy[s]=input[s];}
    }
}
...//output//... 
Still not working, using CodeBlock 10 with GNU C++ Compiler, gcc-c++-4.5.2.1.
See how you get on with this tiny alteration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cctype>
#include <cstring>
#include <algorithm>
#include <iostream>


struct cmpClass{ 
       bool operator()(char a, char b) { return std::tolower(a) < std::tolower(b); } 
     } cmp;

int main()
{
     char input[100] ; 
     std::cin.getline(input, 100) ;
     unsigned inputSize = std::strlen(input) ;
  
     std::sort(input, input+inputSize, cmp) ;
     std::cout << input << std::endl ;
}

Thanks Moschops your code runs well. I forgot if struct should be located outside int main(), it is the same with cire's. Thanks a lot....
Topic archived. No new replies allowed.