reversing letters of words

i am required to reverse the order of the letters of words in the string but the position of punctuation and the words must stay the same. eg.
The dog is big. That is a big dog.
i am supposed to get :
ehT god si gib. tahT si a gib god.
but i get:

.god gib a si tahT .gib si god ehT


any ideas how i could fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>

using namespace std;

int main(){

    string s;

    getline(cin, s);

    string rs(s.rbegin(), s.rend()); // Reversed String

    cout << rs;
    cout << endl;

}
You can do something like this:

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

using std::cout;
using std::endl;
using std::string;
using std::swap;
 
int main()
{
    string text = "The dog is big. That is a big dog.";
    
    int length = text.length();
    
    for (int i = 0; i < length / 2; i++)
    swap(text[i], text[length - i - 1]);
    
    cout << text;
    
    return 0;
}


std::swap: https://www.cplusplus.com/reference/algorithm/swap/

Or more simply, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <algorithm>

using std::cout;
using std::endl;
using std::string;
using std::swap;
 
int main()
{
    string text = "The dog is big. That is a big dog.";
 
    reverse(text.begin(), text.end());
 
    cout << text;
    
    return 0;
}


std::reverse: https://www.cplusplus.com/reference/algorithm/reverse/
Last edited on
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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string backWord( const string &s ){ return string( s.rbegin(), s.rend() ); }

int main()
{
   string s;
   cout << "Enter a sentence: ";   getline( cin, s );
   string word;
   for ( char c : s )
   {
      if ( isalpha( c ) )
      {
         word += c;
      }
      else 
      {
         if ( !word.empty() ) cout << backWord( word );
         word = "";
         cout << c;
      }
   }
   cout << backWord( word );
}
Enter a sentence: The dog is big. That is a big dog.
ehT god si gib. tahT si a gib god. 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cctype>

int main()
{
	//const std::string s {"The dog is big. That is a big dog."};
	std::string s;

	std::cout << "Enter sentence: ";
	std::getline(std::cin, s);

	std::istringstream iss(s);

	for (std::string wrd; iss >> wrd; ) {
		const auto ed {std::find_if(wrd.crbegin(), wrd.crend(), [](unsigned char ch) { return !std::ispunct(ch); })};

		std::cout << std::string(ed, wrd.crend()) << std::string(ed.base(), wrd.cend()) << ' ';
	}

	std::cout << '\n';
}



Enter sentence: The dog is big!! That is a very big dog!!!
ehT god si gib!! tahT si a yrev gib god!!!

Last edited on
I'm trying to read multiple lines from a file and reverse all the lines
input:
Expecting the world to treat you
fairly because you are a good person
is a little like expecting the bull not to
attack you because you are a vegetarian.
output:

kcatta uoy esuaceb uoy era a nairategev.

which is only the last line. how can i correct this?
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
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;

string backWord( const string &s )
{
    return string( s.rbegin(), s.rend() );
}
void revWord(string s)
{
    ofstream outFile;
    outFile.open("output.txt");

    string word;
   for ( char c : s )
   {
      if ( isalpha( c ) )
      {
         word += c;
      }
      else
      {
         if ( !word.empty() )
            outFile << backWord( word );
         word = "";
         outFile << c;
      }
   }
   outFile << backWord( word );
   outFile.close();
}



int main()
{
    ifstream inFile;
    inFile.open("input.txt");
    string s;
    while(!inFile.eof())
    {
        getline( inFile, s );
        revWord(s);
    }
    return 0;
}
Just open the file in main() and leave it open, passing the ofstream as a reference parameter to revWord().

Alternatively, you could let revWord build up a word-reversed string and return that.

Your problems are that:
(1) You are opening and rewriting the file for every line;
(2) You didn't say this was what you wanted to do in your initial post.
Last edited on
You should say what the requirement is at the beginning as per lastchance's issue (2) above.

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 <string>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cctype>

void backword(std::ostream& os, const std::string& line)
{
	std::istringstream iss(line);

	for (std::string wrd; iss >> wrd; ) {
		const auto ed {std::find_if(wrd.crbegin(), wrd.crend(), [](unsigned char ch) { return !std::ispunct(ch); })};

		os << std::string(ed, wrd.crend()) << std::string(ed.base(), wrd.cend()) << ' ';
	}

	os << '\n';
}

int main()
{
	std::ifstream ifs("input.txt");
	std::ofstream ofs("output.txt");

	if (!ifs || !ofs)
		return (std::cout << "Cannot open files\n"), 1;

	for (std::string line; std::getline(ifs, line); backword(ofs, line));
}



gnitcepxE eht dlrow ot taert uoy 
ylriaf esuaceb uoy era a doog nosrep 
si a elttil ekil gnitcepxe eht llub ton ot 
kcatta uoy esuaceb uoy era a nairategev. 

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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;

string confuse( string s )
{
   auto p = s.begin(), q = s.begin();
   for ( ; q != s.end(); q++ )
   {
      if ( !isalpha( *q ) )
      {
         if ( q != p ) reverse( p, q );
         p = q + 1;
      }
   }
   if ( q != p ) reverse( p, q );
   return s;
}

int main()
{
   ifstream in ( "input.txt"  );
   ofstream out( "output.txt" );
   for ( string s; getline( in, s ); ) out << confuse( s ) << '\n';
}

thanks for all the help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main(){
	string s= "The dog is big. That is a big dog.";	
	int p=0, q=0, len =s.size();
	while(1){
		while(!((s[p]>='a' && s[p]<='z') || (s[p]>='A' && s[p]<='Z'))){ p++;}
		q=p;
		while((s[q]>='a' && s[q]<='z') || (s[q]>='A' && s[q]<='Z')){ q++;}	
		if(q>len) break;
		for(int i=p, j=q-1; i<j; i++, j--) swap(s[i], s[j]);			
		p=q+1;
	}
	cout<< s << "\n";
}
Topic archived. No new replies allowed.