Simple String Program

I am creating a program that can count the number of characters and display backwards a string that a user inputs. I got the string length down, but not working with the reverse string. I am getting "error C2440: '=' : cannot convert from 'void' to 'char'" error message. Can anyone please help?
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
//-------------Start of Program------------->
#include <iostream>
#include <cstdlib>
using namespace std;

//---------------String Length Function------------->
int stringLength(char string1[])
{
	int stringCount = 0;
	stringCount = strlen(string1);
	cout << "The number of characters in your string is:  " << stringCount << endl;

}
//------------Reverse String Function---------------->
int reverseString(char string2[])
{
	int x = strlen(string2);
	char stringReverse;
	for(int y = x; y >= (x/2)+1; y--)
	{
		stringReverse = swap(string2[x-y], string2[y-1]);
	}
	cout << "The string backwards is:  " << stringReverse << endl;

}

//-------------Main Function---------------->
int main()
{
	const int INPUT_SIZE = 80;
	char input[INPUT_SIZE];

	//Get the user's desired string
	cout << "Please enter a phrase or sentence:  \n\n";
	cin.getline(input, INPUT_SIZE);  //Read input as a string

	stringLength(input);
	reverseString(input);
	
	return 0;
}

Last edited on
Do you know about std::reverse which does exactly what you want? The problem is that swap doesn't return anything. Why would it?

Since you already use std::swap you might as well learn to use the other algorithms as well.

1
2
3
4
5
6
7
8
#include <algorithm> // don't forget to include the header for algorithm
int reverseString(char string2[]) 
{ 
	int x = strlen(string2); 
	std::reverse(string2, string2 + x); 
	std::cout << " the string reversed is " << string2 << std::endl; 
 
} 
Last edited on
Yeah I know about that, but I'm taking this class and it is making me use a C-Strings. It says I need to create a function that accepts a pointer to a C-String as an argument and displays its contents backwards.

You could just duplicate the std::reverse algorithm code and write the function to take a pointer to the first and one past the end of the array. It is probably better to work with iterators anyway. I'm not sure what you wanted to do with the return value of swap. swap simply swaps two characters and returns nothing. Once finished the string2 is modified in place. So you need to have a first and a last index or iterator which are being used in the swap. Each time they move closer to each other by 1 until the first is >= last.
isn't swap a void function ?
Ok I figured it out. I put it in here because someone else may need the same thing. What do you all think?

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
//-------------Start of Program------------->
#include <iostream>
#include <cstdlib>
using namespace std;

//Function Prototype
void stringLength(char *);  
void reverseString(char *);

//----------------Start Main Function------------------->
int main()
{
	const int INPUT_SIZE = 80;
	char input[INPUT_SIZE];

	//Get the user's desired string
	cout << "Please enter a phrase or sentence:  \n\n";
	cin.getline(input, INPUT_SIZE);  //Read input as a string

	//Display number of characters
	cout << "\nThe entered string is ";
	stringLength(input);
	cout << " characters.  \n";

	//Display string backwards
	cout << "The entered string backwards is:  ";
	reverseString(input);
	cout << endl;
	
}
//<------------End Main Function----------------------->

//<------------Start String Length Function------------>
void stringLength(char *string1)
{
	int stringCount = 0;
	stringCount = strlen(string1);
	cout << stringCount;

}
//<------------End String Length Function-------------->

//<------------Start Reverse String Function----------->
void reverseString(char *string2)
{
	char *revString = string2;

	while(*revString != '\0')
		++revString;

	while (revString != string2)
		cout.put(*--revString);
}
//<-----------End Reverse String Function--------------> 
Its just a thought but do you think your professor might be expecting you to calculate the string length yourself rather than simply calling a function to do it for you?
@kempofighter
Why not help him satisfy the requirements of his homework? He needs to do the reverse function himself, using simple arrays.

@usafsatwide
On line 18 you have a single character named 'stringReverse'. It does not matter what you name a thing, its type is what matters. In this case, you have only one character, which is not a string. A string is many characters, like you have on line 31, where you declare an array of INPUT_SIZE characters.

If I may, however, your function to reverse strings should not perform any output, it should simply reverse the string given to it.

1
2
3
4
5
6
7
void reverseString( char* s )
{
	// This functions reverses the order of the chars in s.
	// You only need two things: the length of the string and an index into
	// the string from either end.
	...
}

Then you can use it simply:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	const int INPUT_SIZE = 80;
	char input[INPUT_SIZE];

	//Get the user's desired string
	cout << "Please enter a phrase or sentence:  \n\n";
	cin.getline(input, INPUT_SIZE);  //Read input as a string

	cout << "The number of characters in your string is:  " << stringLength(input) << ".\n";
	reverseString(input);
	cout << "The reversed string is: " << input << endl;
	
	return 0;
}

One more thing to notice: you have defined a function to determine the length of a string stringLength(), but instead of using it properly, you use strlen() and have your function do what it is supposed to: count the number of characters in the string and return that value.

Hope this helps.

[edit] Well, get called away for a moment and the conversation misses me... [/edit]
Last edited on
Topic archived. No new replies allowed.