Results 1 to 15 of 15

Thread: String.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date: Sep:2013
    Location: Bulgaria
    Posts: 59

    Smile 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;
    }


    ..: - , .

  2. #2

    Join Date: Apr:2006
    Location:
    Posts: 8,666
    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;
    }
    , , - Do not mix C and C++ unless a meteor is coming!

  3. #3
    Registered User
    Join Date: Sep:2013
    Location: Bulgaria
    Posts: 59
    Quote Originally Posted by smelkomar View Post

    , , - Do not mix C and C++ unless a meteor is coming!
    , ++, .

  4. #4
    Registered User mitkohr's Avatar
    Join Date: Jul:2001
    Posts: 1,361
    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

  5. #5

    Join Date: Apr:2006
    Location:
    Posts: 8,666
    Quote Originally Posted by mitkohr View Post
    C++
    "":
    . ?
    . , C++?

  6. #6
    Registered User mitkohr's Avatar
    Join Date: Jul:2001
    Posts: 1,361
    Quote Originally Posted by smelkomar View Post
    "":
    . ?
    . , C++?
    , , , :
    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

  7. #7
    Registered User hateras's Avatar
    Join Date: Jan:2011
    Location: Kronos III
    Posts: 1,028
    Code:
    #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;
    }
    : Mitkohr
    ASRock B550M Pro 4; Ryzen R5 3600; 2x16 GiB G.SKILL Aegis 3200; 1TB Samsung QVO 960 + 3TB Seagate IronWolf; Zalman Z1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |