swapping characters in string array

Hi,
I have an array of strings and there about 20 lines that array. I'm wondering if there is a way I can check the first character of each line and see if one line has a character that comes before the latter line, I can switch them so that in total, I can sort the lines and the entire array alphabetically. Is there a way to do that, to say look at the first character in the line and if it comes later in the alphabet, then switch lines?

I know you can do bubble sort and selection sort but I've literally spent 7 hours today doing that and nothing, I MEAN NOTHING, is working and I asked some classmates and none of their suggestions help.


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
# include <algorithm>
# include <iostream>

int main()
{
    std::string words[ 5 ] = { "aaa", "bb", "aaaa", "cc", "ba" };
   
    std::sort(std::begin(words), std::end(words)); 

    for (auto const& elt: words)
        std::cout << elt << "\n";
}


Is your assignment to implement this on your own, the hard way?
Last edited on
Yes, unfortunately we can't use the sort function and as I said before, all the different sort programs like bubble sort, insertion sort, and selection sort, are literally just not working
Why shouldn't a bubble sort work?
Instead of comparing the whole line you just compare the first char and if necessary swap the lines.
Why don't you show us your code ?
I suggest breaking your assignment to several parts:
* A bool-returning function which tests if c-string a < b (or >).
* A function which swaps two c-strings
* A sort function which uses the above implemented functions.
You don't need to compare each character if you can use <string>
1
2
3
4
5
6
7
8
9
10
11
int main(){

	std::string str1 = "and";
	std::string str2 = "bnd";

	if (str1 < str2) {
		std::cout << "str1 comes before str2, alphabetically\n";
	}
	else
		std::cout << "str2 comes before str1, alphabetically\n";
}

str1 comes before str2, alphabetically\n
Press any key to continue . . .


Bubble sort would work.

Okay, so this is the function I'm working on. Currently, with this code, I can get it to go down to like the fifth lines of the array(there is a total of 20) but it stops right there. I think it's because from the first line to the fifth, everything is already in alphabetical order but then at the sixth line, the sentence starts with an a and needs to be pushed to the first line which this doesn't do.

Also, I'm sure someone will ask why I've done i/j < size +1 in my for loops and the answer is because it doesn't work any other way. Even if I put i/j < max or i/j < max -1 or i/j < size -i - 1, nothing works.

As for where my cout statement is, I also tried putting it outside of that loop and made it its own separate for loop as well but the output is still the same. When I did make that loop though, i still had to put i < size +1 cause nothing else worked.

As for the sort function, I think I might be allowed to use it (I asked my instructor) but everytime I have, it gives me a weird jumble of letters. And honestly, I'd like to keep that sort function as a last resort.

And the question about why the bubble sort wasn't working, I have no clue. I made that bubble sort for another assignment and it did work then, so I know there wasn't a mistake.

If anyone can help me, I'd really appreciate it.


void sortRes(string res[], string cat[], int &s) {
ifstream in;
in.open("myassign.txt");
int const max = 20;
string temp2, temp1, temp;
int i;
if (in.is_open())
{
while (getline(in, temp1))
{
if ((temp1 != "yes") && (temp1 != "no") && (temp1 != "maybe"))
{
for (i = 0; i < size + 1; i++)
{
res[i] = temp1;

for (int j = 0; j < size + 1; j++)
{
if (res[j+1].compare(res[j]) < 0)
{
temp1 = res[j];
res[j] = res[j + 1];
res[j + 1] = temp1;
cout << temp1 << endl;
}
}
}
}

}
}
else {
for (i =0; i < size +1; i++) {
cat[i] = temp;

}
}
}

}

}

Last edited on
Where is size defined and what value does it have ?
Can you show us the input file ?
Why don't you sort the array after you have read all the lines ?
Last edited on
Hello az1234,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



It would be helpful if yo post all your code that can be compiled so everyone knows what you are working with. And include the input file so everyone is using the same information and not having to guess.

int const max = 20;. Generally when you use something like this the variable name is in capital letters. This reminds you that it is defined as a constant.

Hope that helps,

Andy
Topic archived. No new replies allowed.