Results 1 to 15 of 15
Thread: String.
Hybrid View
-
9th October 2013 20:25 #1Registered User
Join Date: Sep:2013
Location: Bulgaria
Posts: 59
String.
, :
.
: - "HardwareBG " - " HardwareBG".
, .
Code:#include "stdafx.h" #include <string> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string myString, Temp, newString; getline(cin, myString); for( ; ; ) { int i = myString.find(" "); Temp = myString.substr(0, i); myString.erase(0, i); newString = newString + " " + Temp; if(myString.find(" ")==0) break; } cout<<newString; return 0; }
..: - , .
-
10th October 2013 03:03 #2
Join Date: Apr:2006
Location:
Posts: 8,666
, , - Do not mix C and C++ unless a meteor is coming!Code:#include <iostream> #include <cstring> #include <cstdio> using namespace std; int main() { cout << "Enter your sentence:" << endl; char cMystring[1024]; gets(cMystring); char * token; token = strtok(cMystring, " "); while (token != NULL) { cout << token << endl; token = strtok(NULL, " "); } return 0; }
-
10th October 2013 10:47 #3Registered User
Join Date: Sep:2013
Location: Bulgaria
Posts: 59
-
10th October 2013 11:40 #4
C++
.
Code:#include <string> #include <iostream> using namespace std; class StringReverser { private: string str; public: StringReverser() { } StringReverser(string str) { this->str = str; } string revese() { int pos1 = 0; int pos2 = 0; string rev = ""; pos1 = pos2 = 0; while((pos1 = str.find(' ', pos2)) != string::npos) { rev = str.substr(pos2, pos1 - pos2) + (pos2 == 0?"":" ") + rev; pos1++; pos2 = pos1; } rev = str.substr(pos2) + (pos2 == 0?"":" ") + rev; return rev; } void readInput() { cout << "Enter a sentence:"; getline(cin,str); } string getStr() { return str; } void setStr(string str) { this->str = str; } }; int _tmain(int argc, _TCHAR* argv[]) { StringReverser rev; rev.readInput(); cout << "Reversing |" << rev.getStr() << "| ---> |" << rev.revese() << "|" << endl; rev.readInput(); return 0; }Gigabyte X570 Aorus Elite, AMD Ryzen 7 5800X@PBO+200, Noctua NH-D15, 2x16GB G.Skill F4-3600C17D-32GTZR, Palit GeForce RTX 4070 Ti GameRock Classic, 2x Sandisk Extreme II 240GB (not in RAID)+WD 320GB AAKS + WD40EZRZ + Toshiba X300 6GB, Cooler Master HAF 922, CORSAIR 750W CX
-
10th October 2013 12:52 #5
-
10th October 2013 15:09 #6
, , , :
, ,Code:myString.erase(0, i); newString = newString + " " + Temp;
. C++ , - ,
.
Code:#include <string> #include <iostream> #include <vector> using namespace std; class Word { private: string word; public: Word() {} Word(string word) { this->word = word; } void setWord(string word) { this->word = word; } string getWord() { return word; } }; class Sentence { private: vector<Word> words; void parseSentence(string sentence) { int pos1, pos2; pos1 = pos2 = 0; while((pos1 = sentence.find(' ', pos2)) != string::npos) { words.insert(words.end(), Word(sentence.substr(pos2, pos1 - pos2))); pos1++; pos2 = pos1; } words.insert(words.end(), Word(sentence.substr(pos2))); } public: Sentence() {} Sentence(string sentence) { parseSentence(sentence); } void setSentence(string sentence) { parseSentence(sentence); } string toString() { string ret = ""; for(vector<Word>::iterator it = words.begin(); it < words.end(); it++) { ret += " " + (*it).getWord(); } return ret; } string toReversedString() { string ret = ""; for(vector<Word>::iterator it = words.begin(); it < words.end(); it++) { ret = (*it).getWord() + (ret.length() == 0?"":" ") + ret; } return ret; } Sentence& reverse() { vector<Word> temp; for(vector<Word>::iterator it = words.begin(); it < words.end(); it++) { temp.insert(temp.begin(), *it); } words = temp; return *this; } friend istream& operator>>(istream& in, Sentence& sent) { string line; getline(in, line); sent.parseSentence(line); return in; } friend ostream& operator<<(ostream& out, Sentence& sent) { for(vector<Word>::iterator it = sent.words.begin(); it < sent.words.end(); it++) { if(it != sent.words.begin()) { out << " "; } out << (*it).getWord(); } return out; } }; int _tmain(int argc, _TCHAR* argv[]) { Sentence s; cout << "Enter a sentence:"; cin >> s; cout << "Reversed sentence is |" << s.reverse() << "|" << endl; cin >> s; return 0; }Gigabyte X570 Aorus Elite, AMD Ryzen 7 5800X@PBO+200, Noctua NH-D15, 2x16GB G.Skill F4-3600C17D-32GTZR, Palit GeForce RTX 4070 Ti GameRock Classic, 2x Sandisk Extreme II 240GB (not in RAID)+WD 320GB AAKS + WD40EZRZ + Toshiba X300 6GB, Cooler Master HAF 922, CORSAIR 750W CX
-
10th October 2013 12:01 #7: MitkohrCode:
#include "stdafx.h" #include <string> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string myString, Temp, newString; getline(cin, myString); // / () // , int i; i = myString.find(" "); while ( i > 0) { Temp = myString.substr(0, i); myString.erase(0, i + 1); // newString = Temp + " " + newString; i = myString.find(" "); } // newString = myString + " " + newString; cout << newString << endl; return 0; }
ASRock B550M Pro 4; Ryzen R5 3600; 2x16 GiB G.SKILL Aegis 3200; 1TB Samsung QVO 960 + 3TB Seagate IronWolf; Zalman Z1




Reply With Quote

Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in