VB icon

Reverse words in a sentence version 1.0

Email
Submitted on: 8/22/2016 12:18:56 PM
By: Ben  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++
Views: 1269
 
     Just shows how to reverse all words in a sentence good example of working with strings for beginners. Hope you find is usfull.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Reverse words in a sentence version 1.0
// Description:Just shows how to reverse all words in a sentence good example of working with strings for beginners. Hope you find is usfull.
// By: Ben
//**************************************

#include <iostream>
#include <Windows.h>
using namespace std;
using std::cout;
using std::endl;
char *StrRev(char *src) {
	int i = strlen(src);
	char *temp = new char[i];
	int j = 0;
	while(i >=0){
		temp[j] = src[i-1];
		j++;
		i--;
	}
	j--;
	temp[j] = '\0';
	return temp;
}
string RevWords(string source){
	char *src = NULL;
	char *temp = NULL;
	string s0 = "";
	char Buffer[128];
	int i = 0;
	int j = 0;
	char c = '\0';
	src = new char[strlen(source.c_str())];
	//Copy string to src
	strcpy(src,source.c_str());
	//Reverse the string
	src = StrRev(src);
	//Below code scans the string looking for a space
	//When a space is found the reversed string is reverse agian
	//The word then is appened to s0 that will become the final output string.
	while(i < strlen(src)){
		if(src[i] == ' '){
			//Add end of string
			Buffer[j] = '\0';
			temp = strrev(Buffer);
			//Add end of string
			temp[j] = '\0';
			s0+= temp;
			//Append the space
			s0+= src[i];
			//Reset buffer counter
			j = 0;
		}
		else{
			//Getting building the string
			Buffer[j] = src[i];
			j++;
		}
		//INC Counter
		i++;
	}
	//This code just adds the remaining last string found in buffer
	Buffer[j] = '\0';
	temp = StrRev(Buffer);
	temp[j] = '\0';
	s0+= temp;
	s0+=c;
	//Clear up
	memset(Buffer,0,sizeof(Buffer));
	memset(src,0,sizeof(src));
	//Return new string
	return s0;
}
int main(int argc, char **anvg){
	string Org = "Microsoft Visual C++ For Windows";
	string s0 = RevWords("Microsoft Visual C++ For Windows");
	SetConsoleTitle(L"Word Reverse By ~DreamVB~");
	cout << "Original: " << Org.c_str() << endl;
	cout << "Reversed: " << s0.c_str() << endl;
	Org.clear();
	s0.clear();
	//Keep console open
	system("pause");
	return 1;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.