Swap function to put string in alphabetical order

So I been working on this c++ project and I need to be able to take three seperate strings and send them to function to put them in alphabetical order through a-z and use the swap function to return them in order. I been searching for problems like this but I haven't fame across any so I'm in need of some help or lead way. I can copy my code onto here as well as a more detailed description of what I'm needing to do onto here if needed.
So I been working on this c++ project and I need to be able to take three seperate strings and send them to function to put them in alphabetical order through a-z and use the swap function to return them in order

By storing your strings in certain C++ containers, such as std::multiset or std::priority_queue, you don't even need manually sort the contents (the containers will sort automatically).

However if your project is homework, you probably are not expected to use these features of C++.

http://www.cplusplus.com/reference/set/multiset/
http://www.cplusplus.com/reference/queue/priority_queue/

I can copy my code onto here as well as a more detailed description of what I'm needing to do onto here if needed.

Please do so.
Here is the word by word of what my teacher want's us to do:
EXERCISE 1  Write a program that works with strings in the following way: 
1)  Ask the user for their name, and store it in a string.  
Also ask where the user lives, and store that information 
in a string as well. 

2) Search for the lowercase letter ‘h’ in the user’s name, 
and have the output say either 
“Your name does not contain the lowercase letter ‘h’.” 
OR “Lowercase letter ‘h’ found in your name at position: etc…” 

3) Next, prompt the user for a letter to count. The SEARCH SHOULD REMAIN 
CASE INSENSITIVE!! (If the user types in uppercase ‘C,’ you will also count 
lowercase ‘c’.) [HINT: Use the toUpper() or toLower() functions]. Print out a 
sentence indicating how many occurrences of the search letter are in the user name. 

4) Then, ask for three strings, alphabetize them using the swap function, and print 
them back out in order. 

5) Concatenate “the great” onto the end of the user name, and print a goodbye message including the name.


Now here is what my code is (I do not have #4 or #5 done however I think I can solve #5 on my own I just need help with #4)

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>

using namespace std;

bool searchforh(string);
int lettercount(string);
char lettersearch;

int main()
{
cout<<"Enter in your name ";
string name;
getline(cin,name);

cout<<"Enter where you live ";

string live;
getline(cin,live);

cin.ignore(80, '\n');
cout<<endl<<endl<<endl<<endl<<"Hello "<<name<<" from the "<<live<<"."<<endl;
cout<<"See how easy strings are!"<<endl<<endl<<endl;
bool outcome=searchforh(name);
if (outcome == true)
{
cout<<"Your name contains the lowercase letter 'h' in it"<<endl<<endl;
}
else
{
cout<<"Your name doesn't contain the lowercase letter 'h' in it"<<endl<<endl;
}

cout<<"Enter in a letter to search in your name ";
cin>>lettersearch;
int outcometwo =lettercount(name);
cout<<"The letter "<<lettersearch<<" appeared "<<outcometwo<<" many times in your name."<<endl;

return 0;
}

//how to lowercase the char so it can then count for all of the instances of it in the name
bool searchforh(string name)
{
int pos=name.find('h');
if (pos == 0)
{
return true;
}
else
{
return false;
}
}

int lettercount(string name)
{
for(int j=0; j<name.length();j++)
{
name[j]=tolower(name[j]);
}
lettersearch=tolower(lettersearch);

int occurance=0;
int pos=name.find(lettersearch);

for(int  i = 0;i < name.length();i++)
{
   if(name[i] == lettersearch)
   {
       occurance++;
   }
}
return occurance;
}

--Edit--
Here is an image link to an example as well as the page of information that I copied from - http://www.mediafire.com/convkey/e118/c4chu5gta031h4ufg.jpg?size_id=8
Last edited on
This is a minor observation, but if that's how you don't indent your code (meaning that your code's indentation wasn't lost while copy-pasting) then you must look into indentation and choose a favorite style immediately:
http://en.wikipedia.org/wiki/Indent_style

Another small observation, why do this:

24
25
26
27
28
29
30
31
32
bool outcome=searchforh(name);
if (outcome == true)
{
cout<<"Your name contains the lowercase letter 'h' in it"<<endl<<endl;
}
else
{
cout<<"Your name doesn't contain the lowercase letter 'h' in it"<<endl<<endl;
}


and not this:

1
2
3
4
5
6
7
8
9
// no extra variable used, and no checking for true or false
if (searchforh(name))
{
    cout<<"Your name contains the lowercase letter 'h' in it"<<endl<<endl;
}
else
{
    cout<<"Your name doesn't contain the lowercase letter 'h' in it"<<endl<<endl;
}


Your searchforh() function seems incorrect to me. I would rewrite it as follows:

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
37
38
39
40
41
42
43
44
45
// refinement 1
bool searchforh(string name)
{
    int pos=name.find('h');

    if (pos != string::npos) // because position will be string::npos if 'h' is not found
    {
        return true;
    }
    else
    {
        return false;
    }
}

// refinement 2
bool searchforh(const string &name) // use const reference for speed
{
    if (name.find('h') != string::npos) // simplify code by using fewer variables
    {
        return true;
    }
    else
    {
        return false;
    }
}

// refinement 3
bool searchforh(const string &name)
{
    // it's almost always silly to return "true" or "false"
    // so when you do that, it usually means you can simplify your code

    return name.find('h') != string::npos;

    // so let's say the name is "Charlie"
    // name.find('h') returns 1 (counting starts from 0)
    // ( 1 != string::npos ) evaluates to true
    // so the final return value is true

    // if the name was "Dan"
    // name.find('h') would return string::npos
    // ( string::npos != string::npos ) evaluates to false
}


4) Then, ask for three strings, alphabetize them using the swap function, and print
them back out in order.

What does it mean to "alphabetize them"? Does your teacher want you to sort the characters in the strings?

If yes, you'll have to write a sort function (or you could use the one provided by C++ named std::sort(), but that would be cheating).
http://www.cplusplus.com/faq/sequences/sequencing/sort-stuff/
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/
I figured it out. If anyone has a question regarding on how to do this here is the total source code that works I believe 100% correct me if I am wrong

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>

using namespace std;

bool searchforh(string);
int lettercount(string);
char lettersearch;

int main()
{
cout<<"Enter in your name ";
string name;
getline(cin,name);

cout<<"Enter where you live ";

string live;
getline(cin,live);

cin.ignore(80, '\n');
cout<<endl<<endl<<endl<<endl<<"Hello "<<name<<" from the "<<live<<"."<<endl;
cout<<"See how easy strings are!"<<endl<<endl<<endl;
bool outcome=searchforh(name);
if (outcome == true)
{
cout<<"Your name contains the lowercase letter 'h' in it"<<endl<<endl;
}
else
{
cout<<"Your name doesn't contain the lowercase letter 'h' in it"<<endl<<endl;
}

cout<<"Enter in a letter to search in your name ";
cin>>lettersearch;
int outcometwo =lettercount(name);
cout<<"The letter "<<lettersearch<<" appeared "<<outcometwo<<" times in your name."<<endl<<endl;

string one, two, three;
cout<<"Enter in three strings! "<<endl;
cin>>one;
cin>>two;
cin>>three;
if (one > three)
{
one.swap(three);
}
if(one > two)
{
one.swap(two);
if (one > three)
{
one.swap(three);
}
}
if (two >three)
{
two.swap(three);

}

cout<<one<<" "<<two<<" "<<three<<endl<<endl;
string thegreat =name+" the great";
cout<<"See ya later, "<<thegreat<<"!"<<endl;
int nummm;
cin>>nummm;
return 0;
}

//how to lowercase the char so it can then count for all of the instances of it in the name
bool searchforh(string name)
{
int occurance=0;
int pos=name.find('h');
for(int  i = 0;i < name.length();i++)
{
  if(name[i] == 'h')
  {
      occurance++;
  }
  if (occurance==0)
  {
  return false;
  }
  if (occurance !=0)
  {
  return true;
  }
}
}

int lettercount(string name)
{
for(int j=0; j<name.length();j++)
{
name[j]=tolower(name[j]);

}

lettersearch=tolower(lettersearch);

int occurance=0;
int pos=name.find(lettersearch);

for(int  i = 0;i < name.length();i++)
{
  if(name[i] == lettersearch)
  {
      occurance++;
  }
}
return occurance;
}
Topic archived. No new replies allowed.