0

I'm a novice programmer learning C++ through a tutorial and I want to know how I can take a string, take its first letter, compare it to the first letters of 2 other strings and then sort them alphabetically. I don't want code written for me. I want to understand how to do this because other solutions were difficult to understand.

I know that strings are just arrays of char so there should be a method to get the first index.

UPDATE

So this is my attempt:

#include <iostream>
#include <string>
using namespace std;

void valueOutput(string firstName, string secondName, string thirdName){
    cout << "\n";
    cout << firstName << endl;
    cout << secondName << endl;
    cout << thirdName << endl;
}

int main(){
    string name1, name2, name3;

    cout<<"Enter 3 names: "<<endl;

    cin>>name1;
    cin>>name2;
    cin>>name3;

    if(
       (name1[0] < name2[0] && name2[0] < name3[0]) 
    || (name1[0] < name3[0] && name3[0] < name2[0])
    || (name2[0] < name1[0] && name1[0] < name3[0])
    || (name2[0] < name3[0] && name3[0] < name1[0])
    || (name3[0] < name1[0] && name1[0] < name2[0]) 
    || (name3[0] < name2[0] && name2[0] < name1[0]))
    {valueOutput(name1, name2, name3);}
    else{
        return 0;
    }

}

My input was: Steinbeck Hemingway Fitzgerald

but the output is the exactly in the same order. I want to sort them alphabetically.

Onur-Andros Ozbek
  • 2,998
  • 2
  • 29
  • 78
  • 4
    as you have noticed yourself, strings just hold an array of chars internally. std::string provides an overload of `operator[]` that gets you the chars in the underlying array. So `str[0]` returns the first character of the string `str`. – Zinki Mar 22 '18 at 16:11
  • Great. Does C++ have a built-in class that compares char values alphabetically?\ – Onur-Andros Ozbek Mar 22 '18 at 16:12
  • What do you mean by _comparing characters alphabetically_? Characters are characters. You can compare one character to another character. You can't compare a character to an array of characters. Explore the [std::string](http://en.cppreference.com/w/cpp/string/basic_string) reference. – Ron Mar 22 '18 at 16:15
  • 1
    I mean, I want to be able to figure out which letter comes first in the alphabet. – Onur-Andros Ozbek Mar 22 '18 at 16:16
  • 1
    And @ downvoter. Can you explain why you downvoted me? I'm genuinely trying to learn. – Onur-Andros Ozbek Mar 22 '18 at 16:16
  • `chars` can be compared directly with operators `>` and `<`, although that uses the underlying numerical value of the char, so `'B'<'a'` is true, because lowercase letters have a higher numerical value than uppercase ones. – Zinki Mar 22 '18 at 16:18
  • As @Zinki says, the '<' and '>' work characters because it converts the character to its ascii integer value e.g 'a' == 97 – Tom Mar 22 '18 at 16:21
  • *other solutions were difficult to understand.* -- Which is highly subjective as to "difficult to understand". What's difficult to you could be child's play to another beginner. What are these "other solutions"? – PaulMcKenzie Mar 22 '18 at 16:22
  • 2
    @OnurOzbek not downvoter; but you'll note that the tooltip for the downvote states "this does not show research effort or is not useful" - so someone likely thinks that you should've done more research. – UKMonkey Mar 22 '18 at 16:24
  • @PaulMcKenzie okay. I think it should be a given that if I am suggesting that something is difficult, that means its difficult to me. Do I really need to spell this thing out? – Onur-Andros Ozbek Mar 22 '18 at 16:31
  • @UKMonkey and I think thats a misuse of the privileges. I posted this to learn because each other answers that I've seen were too advanced for my level. – Onur-Andros Ozbek Mar 22 '18 at 16:32
  • There are unique ASCII code (Which are integers) given to every single characters... So when we compare any two characters then the compiler just compares the ascii code of that character... Like the ascii code of 'a' is 97 and ascii code of 'b' is 98.... so whenever you type a condition like ('a' < 'b') then the compiler assumes it as (97 < 98).... and then return true.... Its simple... Isn't it... We can find the ascii code for every charater here http://www.theasciicode.com.ar/ – letsintegreat Mar 22 '18 at 16:49
  • @OnurOzbek no one questioned your motivation of posting... research you could've done easily is buy a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn it on your own; or write some code and give it a try and see what happened. I'm posting this so that you can understand the thought process that most users will have; rather than discourage you from posting again in the future :) – UKMonkey Mar 22 '18 at 16:50
  • 1
    Thanks mate. Just to note, I am already going through a book. One written by Bjarne himself. – Onur-Andros Ozbek Mar 22 '18 at 16:53
  • 1
    @OnurOzbek I have made an Upvote for showing your effort. It is always usual that the community demotivated the people when they simply ask something without showing a bit of research or the question is not well prepared. Never mid, mistakes are good for learning. –  Mar 22 '18 at 17:19
  • @OnurOzbek... Buddy your updated answer is still wrong.. I suggest you to go with my answer – letsintegreat Mar 22 '18 at 17:23
  • 1
    @HarshitSeksaria. It isn't an "answer". I clearly said that it was an attempt. And I don't want just code. I actually want a decent explanation. – Onur-Andros Ozbek Mar 22 '18 at 17:26
  • Ok I'm updating my code with explaination.... – letsintegreat Mar 22 '18 at 17:26
  • 1
    Now read the explanation of my answer carefully and try to understand what I mean... And if you understand that then tick mark that – letsintegreat Mar 22 '18 at 17:46

8 Answers8

2

I know that strings are just arrays of char so there should be a method to get the first index.

It seems you mean string literals.

If you have three declarations like these

char *s1 = "Onur";
char *s2 = "Ozbek";
char *s3 = "Hello";

then you can compare first characters of the string literals the following ways

if ( s1[0] == s2[0] )

or

if ( *s2 == *s3 )

The same expressions can be used if instead of pointers you declared arrays

char s1[] = "Onur";
char s2[] = "Ozbek";
char s3[] = "Hello";

In fact you can even use the following expressions

if ( "Onur"[0] == "Ozbek"[0] )

or

if ( *"Onur" == *"Ozbek" )

or even like

if ( 0["Onur"] == 0["Ozbek"] )

to compare first characters of string literals directly.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

You can get a particular position using operator[indexNumber]

But if you want to sort, you should use std::sort from #include <algorithm> with a vector of strings std::vector<std::string>

//EDIT: note that a and A are different for a sorting or comparation, search ascii table

I will put a code example do not read further if you do not want the solution

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
  std::vector<std::string> stringArray;
  stringArray.reserve(4);
  stringArray.push_back("Hi");
  stringArray.push_back("Hello world");
  stringArray.push_back("New c++");
  stringArray.push_back("Animal");
  std::sort(stringArray.begin(), stringArray.end());
  for(auto&a : stringArray)
  {
    std::cout << a << '\n';   
  }

}
Ch1v1
  • 121
  • 1
  • 7
1
#include <iostream>

int main() {
    std::string str1, str2, str3;
    std::cout << "Enter First String  :  ";
    getline(std::cin, str1);
    std::cout << "Enter Second String  :  ";
    getline(std::cin, str2);
    std::cout << "Enter Third String  :  ";
    getline(std::cin, str3);
    // The code is descriptive by itself till now

    // Now we gonna compare each of them.
    if(str1[0] < str2[0]) {  // Once the control flow get inside this if statement that means str1 is smaller than str2
        if (str1[0] < str3[0] && str3[0] < str2[0]) 
            std::cout << str1 << std::endl << str3 << std::endl << str2; // In here we get that str1 is smaller than str3 (and str1 is smaller than str2 as well), so the smallest is str1 and we print that. and then we compared str3 with str 2, if str3 is smaller than str2 then we will print secondly str3 and thirdly str2.
        else if (str1[0] < str3[0] && str2[0] < str3[0])
            std::cout << str1 << std::endl << str2 << std::endl << str3; // Here we get that str1 is smaller than str2 and str3 so we firstly print str1 and then we compared str2 with str3, if str2 is smaller than str3 then we will secondly print str2 and thirdly print str3.
        else
            std::cout << str3 << std::endl << str1 << std::endl << str2; // Now both the conditions mentioned above are wrong that means str3 is smaller than str1 and from the very first condition, we get that str1 is smaller than str2. That means smallest is str3 then str1 then str2 and we printed all of them in this order.
    } else { // else will be executed when str2 will be smaller than str1. So till now we get that str2 is smaller than str1. Now remember that str2 is smaller than str1.
        if (str2[0] < str3[0] && str3[0] < str1[0])
            std::cout << str2 << std::endl << str3 << std::endl << str1; // Now here str2 proved to be smaller than str3 (and we already know that str2 is smaller than str1), So str2 is the smallest and we printed firstly str2. Then we compared str3 with str1, if str3 is smaller than str1 then we will secondly print str3 and thirdly print str1.
        else if (str2[0] < str3[0] && str1[0] < str3[0])
            std::cout << str2 << std::endl << str1 << std::endl << str3;  // Now here str2 proved to be smaller than str3 (and we already know that str2 is smaller than str1), So str2 is the smallest and we printed firstly str2. Then we compared str1 with str3, if str1 is smaller than str3 then we will secondly print str1 and thirdly print str3.
        else
            std::cout << str3 << std::endl << str2 << std::endl << str1; // Now both the above conditions are false that means str3 is smaller than str2 and as we know that str2 is smaller than str1, that means str3 is the smallest, so we firstly printed str3 and then str2 and at last str1.
    }
return 0;
}

This is whole code I think that you can understand it... And if you cant understand any particular line, you can freely ask..

letsintegreat
  • 3,328
  • 4
  • 18
  • 39
1

If you are aware of STL container std::vector, this job is much easier. If you are looking for something simple, here it is, hopes that the comments help you to understand.

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
int main()
{
    int size = 3;                 //size of your vector
    std::vector<std::string> vec; //initialize the vec with string type
    vec.reserve(size);            // just to reserve the memory

    // input iterator takes strings from std console-size times-and put it back in to vec
    std::copy_n(std::istream_iterator<std::string>(std::cin), size, back_inserter(vec));

    // this will do your job as per requirement
    std::sort(vec.begin(), vec.end());

    // just to print the result
    std::copy(vec.begin(),vec.end(), std::ostream_iterator<std::string>(std::cout,"\n"));
    return 0;
}
1

Lets suppose that your strings are named st1 , st2 , st3 and you want to perform the above mentioned operation on them.

A quick way to do it would be using std::vector. You push all string first index values to the vector and then sort it.A clear cut implementation would be something like this :

  vector<char> v;
  v.push_back(st1[0]);
  v.push_back(st2[0]);
  v.push_back(st3[0]);

  sort(v.begin() , v.end());

You can read more about vectors here : https://www.geeksforgeeks.org/vector-in-cpp-stl/

Varad Bhatnagar
  • 599
  • 1
  • 7
  • 19
  • Why vector? How does it relate to sorting alphabetically? – Onur-Andros Ozbek Mar 22 '18 at 17:07
  • The elements inside a vector are sorted based on their values which in this case is the ASCII values. So they you get sorted in increasing order of ASCII values i.e. alphabetically. Here is an ASCII value table link for reference : https://www.asciitable.com/ – Varad Bhatnagar Mar 22 '18 at 17:09
  • 1
    How do I output this sort? – Onur-Andros Ozbek Mar 22 '18 at 17:14
  • Questioner just want to know that how to compare two characters. He didn't know that there is something called ASCII Code. And you're telling him to use vector. I think that you are doing wrong. – letsintegreat Mar 22 '18 at 17:17
  • I would suggest that you go through the link that I gave you, as vector has a lot of utility. The proper way to output it is using an iterator , details of which you can find the link but for this problem you can do something like cout< – Varad Bhatnagar Mar 22 '18 at 17:18
  • 1
    @OnurOzbek. Through this answer you can just print the first letters of the string in order.... This Answer is of no use – letsintegreat Mar 22 '18 at 17:18
  • @VaradBhatnagar As you can see in my attempt, I am already comparing it with my if statement. – Onur-Andros Ozbek Mar 22 '18 at 17:20
  • @HarshitSeksaria I think a vector is something which can be used to compare a lot more alphabets. Therefore it is more scalable and generic. – Varad Bhatnagar Mar 22 '18 at 17:22
  • @OnurOzbek You have to compare each and every character in your solution which is correct but not efficient. std:sort() uses better algorithms which minimizes the number of comparisons required. – Varad Bhatnagar Mar 22 '18 at 17:25
  • But Onur wanna know how to compare characters. So I think that firstly you should answer that and then you can tell me about vectors as a bonus for him.. – letsintegreat Mar 22 '18 at 17:25
  • @OnurOzbek I hope you get the point that you'll have to compare for each combination of characters if you use the if statement. So if your problem is extended to 5 strings , you'll have to carry out 10 comparisons to ascertain the correct alphabetical order. I hope I am making myself clear. – Varad Bhatnagar Mar 22 '18 at 17:28
  • I still appreciate the effort, @VaradBhatnagar – Onur-Andros Ozbek Mar 22 '18 at 17:54
  • Great efforts @VaradBhatnagar, but this solution will not print the strings, it will just give you the first characters of each string in ascending order. – Nikhil Wagh Mar 22 '18 at 18:22
1

If you don't want the code, I'll just tell you the logic to do the same.

1. Hashing

You can use hashing to sort your strings. The idea behind this is that at the position of the first character of your (i)th string you store i. For eg: consider your strings are as follows :: aaa, bbb, ccc

Now at position [ith_string[0] - 'a'] you store i (here 0 position corresponds to 'a', 1 position corresponds to 'b',.. and so on).

In other words you do this array[str1[0] - 'a'] = 1, and so on for every string.

So your array will look like this :: array = {1, 2, 3}

Then you can just print the string using the position you stored in our hashing array. I know this seems a bit difficult, but I would suggest you see some tutorials about hashing, you'll understand it then.

2. Sorting

You can also use sorting, but then you need to store the location of your string. You can use pair to store the string and the location of your string. Then you can use @VaradBhatnagar's solution to sort the strings and depending on the position, you can print it in that order.

In case you're wondering what I just said, please see the below code for reference. I'm sure you'll understand this.(If you don't please feel free to ask doubts).

#include <bits/stdc++.h>
using namespace std;
void hashing(string str[], int n){
    int arr[26] = {0}; // Because there are only 26 alphabets
    for(int i = 0 ; i < n ; i++){
        arr[str[i][0] - 'a'] = i + 1; // we should not store 0, as 0 defines "there was no string starting from this alphaber" 
    }
    for(int i = 0 ; i < n ; i++){
        if(arr[i] != 0){
            cout << str[arr[i] - 1] << endl;
        }
    }
}   
void sorting(string str[], int n){
    std::vector<pair <char, int> > v;
    for (int i = 0 ; i < n ; i++){
        v.push_back(make_pair(str[i][0], i));
    }
    sort(v.begin(), v.end()); // this will sort on the basis of 1st argument in our pair
    for(int i = 0 ; i < n ; i++){
        cout << str[v[i].second] << endl;
    }
}
int main(int argc, char const *argv[])
{
    int n;
    string str[30]; // it can not be larger than 26, because of the question
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin >> str[i];
    }
    cout << "Result of hashing" << endl;
    hashing(str, n);

    cout << "Result of sorting" << endl;
    sorting(str, n);

    return 0;
}

Input

3
cccc
bb  
aaaa 

Output

Result of hashing
aaaa
bb
cccc
Result of sorting
aaaa
bb
cccc
Nikhil Wagh
  • 1,376
  • 1
  • 24
  • 44
0

Figured it out. I basically had to call the function at each if statement:

#include <iostream>
#include <string>
using namespace std;

void valueOutput(string firstName, string secondName, string thirdName){
    cout << "\n";
    cout << firstName << endl;
    cout << secondName << endl;
    cout << thirdName << endl;
}

int main(){
    string name1, name2, name3;

    cout<<"Enter 3 names: "<<endl;

    cin>>name1;
    cin>>name2;
    cin>>name3;

    if(name1[0] < name2[0] && name2[0] < name3[0]){
        valueOutput(name1, name2, name3);
    }
    else if(name1[0] < name3[0] && name3[0] < name2[0]){
        valueOutput(name1, name3, name2);
    }
    else if(name2[0] < name1[0] && name1[0] < name3[0]){
        valueOutput(name2, name1, name3);
    }
    else if(name2[0] < name3[0] && name3[0] < name1[0]){
        valueOutput(name2, name3, name1);
    }
    else if(name3[0] < name1[0] && name1[0] < name2[0]){
        valueOutput(name3, name1, name2);
    } 
    else if(name3[0] < name2[0] && name2[0] < name1[0]){
        valueOutput(name3, name2, name1);
    }

}
Onur-Andros Ozbek
  • 2,998
  • 2
  • 29
  • 78
0

enter image description hereLets suppose your 3 names is name1 and name2 and name3 and you know that string is just an array of char so you can compare each char using its Ascii number and dont forget to take in consideration the capital and small letters as the difference between them in ascii is 32 and small litters is greater than capital letters so you will convert between them as in code. there isn't any complex function here so it's so easy to understand

#include <iostream>
using namespace std;

void comparing(string& name1,string& name2);
void comparing(string& name1,string& name2,string& name3);

int main(){
    string name1, name2, name3;
    cout<<"Enter 3 names: "<<endl;
    cin>>name1;
    cin>>name2;
    cin>>name3;
    comparing(name1,name2,name3);
}
void comparing(string& name1,string& name2)
{
    string t;
    for(int i=0;i<name1.size();i++){
        int x = (int)name1[i];
        int y = (int)name2[i];
        if(x<=97 && x>=122){
            x=x-32;
        }
        if(y<=97 && y>=122){
            y=y-32;
        }
        if(x>y){
            t=name1;
            name1=name2;
            name2=t;
            break;
        }}
}
void comparing(string& name1,string& name2,string& name3)
{
    comparing(name1,name2);
    comparing(name2,name3);
    comparing(name1,name3);
    comparing(name1,name2);
    cout << name1 << " " <<name2 << " " <<name3 <<endl;
}
  • Can't verify the accuracy of this program. If I input 'bca', cba', 'abc' I expect to get back an alphabetical ordering on the first character. Your code do not do that. Please include example input and output to help clarify what your code does. – pastaleg Mar 25 '18 at 22:37
  • if inputs is ( 'bca' , 'cba' , 'abc' ) then the ouputs is ( abc , bca , cba. ) – Nada Nasser Mar 26 '18 at 19:14
  • you can see the input and output example in the picture posted with the edited answer – Nada Nasser Mar 26 '18 at 19:22