c++ program to reverse a word (without algorithms)

Write your question here.
Hello everyone I have to write a program to reverse a sentence like
cat
tac
but my program is case sensitive. Like if i enter sentence it gives me ecntenes(which is wrong). Can someone help me how to make it non case-sensitive using vectors? Thank you. (I cannot use algorithms because for my assignment i am not allowed to)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  Put the code you need help with here.
// BackWardsSentence.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string BackWords (string sentence);
int BackSentence (string sentence);
string BackWords1 (string sentence);

int _tmain(int argc, _TCHAR* argv[])
{
	string sentence;
	int choice;
	cout<< "What is your sentence" << endl;
	getline (cin,sentence);
	cout<< "You entered" << " "<< sentence;
	cout<< " "<<"If you would like to reverse letters enter 0 if you would like to reverse words enter 1"<<endl;
	cin>> choice;
	if(choice==0)
	{
	cout<< "Your new sentence is " << " " <<BackWords(sentence)<< endl;
	}
	if(choice==1)
	{
		cout<< "Your new sentence is" <<" "<<BackSentence(sentence)<<endl;
	}
	return 0;
}

string BackWords (string sentence)
{
	int length= sentence.length(); //3
	int x=0;
	int y=length-1; //2
	int a=0;
	while (x<length-1) //x<3
	 {
		string sv;
		string sb;
		string sy;
		char const v=sentence.at(x); 
		char const b=sentence.at(y);
		sv = (v);
		sb = (b);
		if(x==0)
		{
		sentence.replace(x,1,sb);
		sentence.replace(y,1,sv);
		}
		if (x==1)
		{
			sentence.replace(x,1,sb);
			sentence.replace(y,1,sv);
		}
		x++;
		y--;
	}	
	return sentence;
}

/*string BackWords1 (string sentence) {
	int start = 0;
	int end = sentence.length() - 1;
	while (start < end) {
		char temp = sentence[end];
		sentence[end] = sentence[start];
		sentence[start] = temp;
		++start;
		--end;
	}
	return sentence;
}*/


int BackSentence (string sentence)
{
	int length = sentence.length();

	return length;
}
Last edited on
What do you mean by it being case sensitive? You're just reversing the order of the characters, do you want to make them all lowercase or something?
I don't exactly know what you mean by case sensitive, but here's an easy way to make a string all lower case letters by including <algorithm> and <string>.

1
2
std::string data = "Abc"; 
std::transform(data.begin(), data.end(), data.begin(), ::tolower);

Source: http://stackoverflow.com/questions/313970/stl-string-to-lower-case

For reversing a string, you can use the reverse function in <algorithm>.
http://en.cppreference.com/w/cpp/algorithm/reverse

For reversing words you can use this function that I wrote as a solution to a problem on a different thread in this forum.

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

std::string reverseSentence(std::string sentence) {
	std::string reversedSentence;
	int words{1};
	std::vector<std::string> word(words);
	for (size_t i = 0; i < sentence.length(); i++) {
		if (sentence[i] != ' ') {
			word[words - 1] += sentence[i];
		}else {
			words++;
			word.resize(words);
		}
	}
	std::reverse(word.begin(), word.end());
	for (auto &curWord : word) {
		reversedSentence.append(curWord + " ");
	}
	return reversedSentence;
}

int main() {
	std::string sentence = "This is a sentence";
	std::cout << reverseSentence(sentence) << "\n";
        std::cin.get();
	return 0;
}


And NT3's solution to the same problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <deque>

void reversedString(const std::string& in) {
    std::istringstream iss (in);
    std::deque<std::string> words;
    std::copy(std::istream_iterator<std::string>(iss), 
              std::istream_iterator<std::string>(), 
              std::front_inserter(words));
    std::copy(std::begin(words), std::end(words), 
              std::ostream_iterator<std::string>(std::cout, " "));
    std::cout << std::endl;
}

int main() {
    reversedString("This is a sentence");
    reversedString("Program");
    reversedString("You are great");
    return 0;
}


Link to forum post: http://www.cplusplus.com/forum/general/132918/
Last edited on
Thank you so much the reverse really worked!!!!
Topic archived. No new replies allowed.