For loops and swapping characters in arrays. (help please)

so basically I have to make a function that will swap a character with a character directly to its right.

1
2
3
4
5
6
7
8
void swap (char letters[11], int current)
{
//swap swaps the character at the index current with the 
//character immediately to its right.  If the current is
//at the end of the array, an error message is
//printed.  Otherwise, a successful swap message is 
//printed.
}


thats the function I have to use, im new with arrays and not exactly sure what to do, but I know it requires a for loop but I can't seem to get it.
help would be greatly appreciated, can someone point me in the right direction please!!! thanks.
It does not require a for loop.

At most, you would have an if-else structure and one variable.
Last edited on
so if I did this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void swap (char letters[11], int current)
{
int hold;

hold = letters[current];
letters[current] = letters[current + 1];
letters[current + 1] = hold;

if (current > current + 1)
{
cout << "Too close to the end of the array, cannot swap!" << endl;
}
else
{
cout << "Swap completed" << endl;
}
}


does that look about right?
Topic archived. No new replies allowed.