Reversing a word

We just started c-strings in a summer class I'm taking and part of my assignment is the following:

Create a function that will reverse the characters in a C-string. The function prototype is: void reverse (char word[]).

-The reverse function cannot have any work arrays – no local variables that are arrays.
-The algorithm for the function:
-Create two integer variables, left and right, that will serve as indexes for the work string.
-Find the null character (‘\0’) in the string and set right to be one less that the index where the null character was found.
-Set the left variable to 0.
-Set up a while-loop that will swap the character at location left with the character at location right, then update the index variables to access the next characters.

Since we just started this today (and it's due tomorrow) I'm struggling a bit. Here's the definition of the function which I have so far, which appears to be worthless. Any help would be appreciated, thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 void reverse (char word[])
{
	int left = 0;
	int right = strlen(word) -1;
	char a = word[0];
	char b = strlen(word) - 1;
	
	while (left < right)
	{
		char temp = a;
		a = b;
		b = temp;
		right--;
		left++;
	}

	
}
You need to use somethng like
1
2
3
4
5
6
7
8
9
10
11
int i, left, right;
char a, b;
for(i=0;i<(strlen(word)/2);i++)
{
    left=i;
    right=strlen(word)-i;
    a=word[left];
    b=word[right];
    //swap a and b

}

And change that for to while if you asked to use while loop.
Last edited on
Thanks Lendra! Unfortunately we have to use a while loop. I'm terrible at this.
Last edited on
Just move the i=0 before the loop and the increment inside loop
Thank you, Lendra! One question - why divide the strlen(word) by 2?
If you swap characters for the entire length of the string, it would be reversed twice getting you back to where you started.
Thanks so much! Here's what I have now, which is still not working:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void reverse (char word[])
{
	int i = 0;
	int left = 0;
	int right = strlen(word) -1;
	char a, b, c; 
	
	
	while (i < strlen(word) / 2)
	{
		left = i;
		a = word[left];
		b = word[right];
		c = a;
		a = b;
		b = c;
		i++;
	}
You aren't changing the value of 'right', so it will always be swapping with the last char in the array. Change 'word[right]' to 'word[right-i]'.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

std::string reverse(std::string);

int main()
{
    std::string str = "hello, world!";
    
    std::cout << reverse(str) << std::endl;
    
    return 0;
}

std::string reverse(std::string str)
{
    return std::string(str.rbegin(), str.rend());
}
!dlrow ,olleh

Is probably the easiest way to reverse a string if you are allowed constructors and iterators.

To do it the way you are you basically start with the first and last position and swap them and increment/decrement till they meet in the middle (if it is odd number of characters) or when one is greater than the other then end.
Another way would be left = 0, right = size - 1 (since we start at 0) and iterate till left is less than half if it is odd then it will leave the middle alone.
Something like:

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
#include <iostream>
#include <string>

std::string reverse(std::string);

int main()
{
    std::string str = "hello, world!";
    
    std::cout << reverse(str) << std::endl;
    
    return 0;
}

std::string reverse(std::string str)
{
    for(int i = 0, j = str.size()-1; i < str.size() / 2.0f; ++i && --j)
    {
        char temp = str[i];
        str[i] = str[j];
        str[j] = temp;
    }
    
    return str;
}
!dlrow ,olleh


The problem with yours is 1) you have arbitrary variable i instead of using left, you don't need variables b and c, and you never assign to the actual array it should be something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void reverse (char word[])
{
	int left = 0;
	int right = strlen(word) -1;
	char a;
	
	
	while (left < strlen(word) / 2)
	{
		a = word[left];
		word[left] = word[right];
                word[right] = a;
		++left;
                --right;
	}
}
Last edited on
Tried that and still getting jargon. passing the input to the function. What appears isn't even text.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void reverse (char word[])
{
	string workstring = word;
	int i = 0;
	int left = 0;
	int right = strlen(word) - 1;
	char a, b, c;

	while (i < strlen(word) / 2)
	{
		left = i;
		a = word[left];
		b = word[right];
		c = a;
		a = b;
		b = c;
		i++;
		word[right - i];
	}
}
That is because you have a bunch of unnecessary variables and you never modify the word. You also have this?? word[right - i]; which literally does nothing. I edited my last post also since I forgot some information that I felt was useful though you may, or may not find it useful.
Last edited on
Thank you, Giblit. Unfortunately I have to use the given prototype - void reverse (char word[]) using a while loop.
Go for it. Anyways, you were extremely close with your original code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void reverse (char word[])
{
	int left = 0;
	int right = strlen(word) -1;
	char a = word[0];
	char b = strlen(word) - 1;
	
	while (left < right)
	{
		char temp = a;
		a = b;
		b = temp;
		right--;
		left++;
	}
You just had to make a few changes: 1) change the value of the array and get rid of useless a & b variables. something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
void reverse (char word[])
{
	int left = 0;
	int right = strlen(word) -1;
	
	while (left < right)
	{
		char temp = word[left];
                word[left] = word[right];
                word[right] = temp;
		right--;
		left++;
	}


*typo
Last edited on
That did the trick! Thank you so much, Giblet and everyone else. I have no doubt it can be frustrating dealing with someone as pathetic at C++ such as myself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void reverse (char word[])
{
	int left = 0;
	int right = strlen(word) - 1;
	
	while (left < right)
	{
		char temp = word[left];
		word[left] = word[right];
		word[right] = temp;
		left++;
		right--;
	}
	cout << word;
	
}
Now for my next challenge, a make plural function - void makePlural (const char singular[], char plural[]);

CRY
Should be fairly simple either append s, es, or ies.
He always has funny instructions. If the word ends with an ‘s’, ‘x’, ‘sh’, or ‘ch’, add ‘es’ to make it plural. Otherwise, simply add the letter ‘s’.

Here's what I have, which breaks the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void makePlural (const char singular[], char plural[]) //In the word ending with an‘s’, ‘x’, ‘sh’, or ‘ch’, add ‘es’ to make it plural. Otherwise, simply add the letter ‘s’.
{
	int n;
	n = strlen(singular);
	char ch = singular[n - 1];
	char ch2 = singular[n - 2];

	if (ch == 's' || ch == 'x')
	{
		strncpy(plural, singular,'es');
	}
	else if (ch == 'h' && ch2 == 's')
	{
		strncpy(plural, singular, 'es');
	}
	else if (ch == 'h' && ch2 == 'c')
	{
		strncpy(plural, singular, 'es');
	}
	else
	{
		strncpy(plural, singular, 's');
	}
}
I think I got it.

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
void makePlural (const char singular[], char plural[]) //In the word ending with an‘s’, ‘x’, ‘sh’, or ‘ch’, add ‘es’ to make it plural. Otherwise, simply add the letter ‘s’.
{
	int n;
	n = strlen(singular);
	char ch = singular[n - 1];
	char ch2 = singular[n - 2];

	if (ch == 's' || ch == 'x')
	{
		strncpy(plural, singular, 23);
		strncat(plural, "es", 23); 
	}
	else if (ch == 'h' && ch2 == 's')
	{
		strncpy(plural, singular, 23);
		strncat(plural, "es", 23);
	}
	else if (ch == 'h' && ch2 == 'c')
	{
		strncpy(plural, singular, 23);
		strncat(plural, "es", 23);
	}
	else
	{
		strncpy(plural, singular, 23);
		strncat(plural, "s", 23);
	}
}
If the array has extra padding you can simply replace the null with the es/s and then shift it right 1 or 2 places. By the way what is up with the arbitrary 23? You can also combine all the es'es into one if by doing
1
2
3
4
if((ch == 's' || ch == 'x') || (ch == 'h' && ch2 == 's') || (ch == 'h' && ch2 == 'c'))
{
    //append the es
}
Last edited on
I chose 23 to add 2 extra characters to the array for the 'es.' It's something our professor did for another section of the homework so I replicated. Was probably pointless, but I don't really know what I'm doing.

Thanks again for all of your help, it is greatly appreciated!
Registered users can post here. Sign in or register to post.