Hello

I have 2 functions that are just playing up. One is a function that takes in a string & should output the string backwards.
Problem: the syntax to insert a character into the string is wrong, I dont know the correct syntax.

The other is a function that takes in a string & converts each character to uppercase.
Problem: I dont know, as far as I can see, there is nothing wrong with the code, but it still outputs the string in a lowercase form, when it should be in uppercase

Any help would be greatly appreciated

Function 1:

string reverse(string s) {

	int size = (int)s.length();


	for (int i=0; i<size; i++) {
		s[i].insert( s[size-1] ); // I get an error here???
		s.erase(size);
	}
               return s;

Function 2:

string makeUpper(string s) {

	for (int i=0; i<(int)s.length(); i++) {
		if ( islower(s[i]) ) {
			toupper(s[i]);
		}
	}

	return s;
}

Code to call the functions:

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int countUpper(string s); // Task 1
string reverse(string s); // Task 2
string makeUpper(string s); // Task 3

int main() {

	string b = "abcde";
	string c = "abcde";

	cout << "String b reversed = " << reverse(b) << endl;
	cout << "String b capitalised = " << makeUpper(c) << endl;

	return 0;
}

Recommended Answers

All 4 Replies

First problem:

int main()
{
    string h = "abcde";
    std::reverse(h.begin(), h.end());

The problem with the second function is that the parameter is passed by value instead of by reference.

The first error:
s - is not a string, is a char
Second:
inserting must be into begin, not into the end, so code

s[i].insert( s[size-1] );

is fully incorrect
Third:
Function insert has many overloads, but it's no string insert(char);

Correct code

string reverse(string s)
{
	int size = (int)s.length();

	char temp[2];
	for(int i = 0; i < size; i++)
	{
		temp[0] = s[size-1]; //keep symbol from the end
		temp[1] = '\0';
		s.insert(i, string(temp)); //inserting into begin
		s.erase(size); //erase last symbol
	}
	return s;
}

Function 2:

the 5th line should be:
s = toupper(s);

toupper() fucntion is returning a value, it is not directly manipulating the string, so you still need to assign the current character to the returned value.

Hope it helps.

This might be of help. Much easier way of reversing a string.

//note using mathematical notation its  [strt,end] and not [strt,end).
//strt is included and end is included as well.

void reverseRecursivly(string& str,int strt, int end)
{
	if(strt >= end || !str[0]) return;
	
	std::swap(str[strt],str[end]);
	reverseRecursivly(str,strt+1,end-1);
}

And also your makeUpper function is wrong :

string makeUpper(string s) {

	for (int i=0; i<(int)s.length(); i++) {
		if ( islower(s[i]) ) {
			s[i] = toupper(s[i]); //Revised
		}
	}

	return s;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.