I have the string
String str = "my age is how old i am?how old are you?";
And I want to extract just the string "how old are you" and place it in another variable.
How can I do this?
You can use the substring method:
http://www.cplusplus.com/reference/string/string/substr/
int first = str.find('?')
String ageStr = str.substr(first, str.find('?', first) - first);
This way you select the part of the string between the position of the first question mark, and the position of the second question mark...
#include <string>
#include <iostream>
using namespace std;
string GetStringInBetween(string Src,string FirstTag, string Secondag)
{
size_t FirstPos,SecondPos;
string newstr="";
FirstPos = Src.find(FirstTag);
if (FirstPos != string::npos)
{
FirstPos++;
SecondPos = Src.find(Secondag, FirstPos );
if (SecondPos != string::npos)
{
newstr = Src.substr(FirstPos , SecondPos - FirstPos);
}
}
return newstr;
}
int main()
{
string str = "?how old are you?";
string nstr = GetStringInBetween(str, "?", "?");
cout << nstr;
return 0;
}
for(int i=0;i<strlen(str);i++)
{
if(str[i] != '?')
{
cout<<str[i];
}
}
You can add any symbol of your choice in place of ?.
Related
How to remove all instances of the pattern from a string?
string str = "red tuna, blue tuna, black tuna, one tuna";
string pattern = "tuna";
Removes all instances of the pattern from a string,
#include <string>
#include <iostream>
using namespace std;
void removeSubstrs(string& s, string& p) {
string::size_type n = p.length();
for (string::size_type i = s.find(p);
i != string::npos;
i = s.find(p))
s.erase(i, n);
}
int main() {
string str = "red tuna, blue tuna, black tuna, one tuna";
string pattern = "tuna";
removeSubstrs(str, pattern);
cout << str << endl;
}
This is a basic question and you'd better take a look at the string capabilities in the standard library.
Classic solution
#include <iostream>
#include <string>
int main() {
std::string str = "red tuna, blue tuna, black tuna, one tuna";
std::string pattern = "tuna";
std::string::size_type i = str.find(pattern);
while (i != std::string::npos) {
str.erase(i, pattern.length());
i = str.find(pattern, i);
}
std::cout << str;
}
Example
RegEx solution
Since C++11 you have another solution (thanks Joachim for reminding me of this) based on regular expressions
#include <iostream>
#include <string>
#include <regex>
int main() {
std::string str = "red tuna, blue tuna, black tuna, one tuna";
std::regex pattern("tuna");
std::cout << std::regex_replace(str, pattern, "");
}
Example
Try something like:
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
From Replace part of a string with another string
A faster algorithm building the string one character at a time and checking if the end matches the substring. The following runs in o(substring * string) where as the above solutions run in o(s^2/t).
string S, T;
cin >> S >> T;
/* Build R, the result string, one character at a time. */
string R;
for (int i = 0; i < S.size(); i++) {
R += S[i];
/* If the end of R matches T then delete it. */
if (R.size() >= T.size() && R.substr(R.size() - T.size()) == T) {
R.resize(R.size() - T.size());
}
}
cout << R << endl;
This is the basic logic for better understanding
Here is the code in c++
string s,x; //s is the string , x is the substring
int a,l;
cin>>s>>x;
l=x.length();
while(true)
{
a=s.find(x);
if(a==-1)
{break;} // if substring is not found
else
{
s.erase(a,l); //if substring is found
}
}
if(s.length()==0) // if string length becomes null printing 0
cout<<0<<"\n";
else
cout<<s<<endl; // else printing the string
example:
input-shahaha
output-shha
There are a few examples about this question. However most of the answers are not what I am looking for.
I am looking for a way to implement an efficient and easy function rather than using boost or any other non STL libraries. If you ask me why, in most coding competitions and interviews, you are not allowed to use them.
Here is the closest that I can approach:
vector<string> SplitString(const char *str, char c)
{
vector<string> result;
do {
const char *begin = str;
while(*str != c && *str) {
str++;
}
result.push_back(string(begin, str));
} while (0 != *str++);
return result;
}
int main() {
string mainString = "This is a sentence. Another sentence. The third sentence. This is the last sentence.";
vector<string> sentences;
sentences = SplitString(mainString.c_str(), '.');
while (!sentences.empty()) {
cout << sentences.back() << endl;
sentences.pop_back();
}
return 0;
}
Now the problem with this is, it can only have a char delimiter not string. I have thought of implementing a few ways but they seemed way too complex. The easiest one that I thought was, convert delimiter to char array use c as the first char of the delimiter char array after this:
while(*str != c && *str) {
str++;
}
const char *beginDelim = *cArr;
while(1) {
if (*str == *cArr && *str && *cArr) {
str++;
cArr++;
}
else if (!*cArr) {
break;
}
else if (*cArr) {
cArr = beginDelim;
}
}
And the code continues from result.push_back() part.
So I was wondering if are there any way to implement an efficient and easy function for splitting a string with a string delimiter?
Generally speaking, a string is a char pointer. So you should search for the first character in the delimeter, then check the very next character. Also in looking at your code I am not sure that while (0 != *str++) is doing what you think it is. I think you mean for it to be null terminated.
something like this should do it:
vector<string> SplitString(const char* str,const char* d) {
vector<string> result;
size_t len = strlen(d);
const char* start = str;
while ( str = strstr(start,d) ) {
result.push_back(string(start,len));
start = str + len;
}
result.push_back(start);
return result;
}
How's this:
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
vector<string> SplitString(const string &str, const string &delim)
{
vector<string> ret;
string::const_iterator prev = str.begin();
for (string::const_iterator i = str.begin(); i < str.end() - delim.length()+1; ++i)
{
if (equal(delim.begin(), delim.end(), i)) {
ret.push_back(string(prev,i));
i += delim.length()-1;
prev = i+1;
}
}
ret.push_back(string(prev,str.end()));
return ret;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> SplitString(string str, const string &delim) {
vector<string> result;
size_t found;
while((found = str.find(delim)) != string::npos) {
result.push_back(str.substr(0, found));
str = str.substr(found + delim.size());
}
return result;
}
int main() {
string mainString = "This is a sentence. Another sentence. The third sentence. This is the last sentence.";
vector<string> sentences;
sentences = SplitString(mainString, ".");
for(auto& sentence : sentences) {
cout << sentence << endl;
}
return 0;
}
vector<string>split(string str, const char d){
string temp;
vector<string>vct;
for(int i = 0; str[i] != '\0'; i++){
if(str[i] != d){
temp += str[i];
}else if(!empty(temp)){
vct.push_back(temp), temp.clear();
}
}
vct.push_back(temp);
return vct;
}
Takes two arguments
const char d as delimiter.
string str as string to be splitted.
stores splitted string in a vector and returns it.
Although, I'm not sure about efficiency of this code. :)
#include <iostream>
#include <vector>
using namespace std;
void RevStr (char *str)
{
if(*str !=0)
{
vector<char> v1;
while((*str != ' ')&&(*str !=0))
v1.push_back(*str++);
// trying to not add space in the last word of string
if(*str !=0)
{
v1.push_back(' ');
str++;
}
RevStr(str);
cout<<*str;
}
}
int main()
{
RevStr("hello world!");
cout<<endl;
}
I want to change the order of words in the string for example " how are you" => "you are how"
I am having some problem, its not printing correctly (print only w), please help me and tell me what i did wrong. However i know that I should not call "cout<<*str;
" since i am inserting the "array of char" in stack (recurssion) but i dont know what i need to do.
C++ makes it simple:
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
std::string reverse(std::string const& text)
{
std::stringstream inStream(text);
std::stringstream outStream;
std::vector<std::string> words;
std::copy(std::istream_iterator<std::string>(inStream), std::istream_iterator<std::string>(), std::back_inserter(words));
std::copy(words.rbegin(), words.rend(), std::ostream_iterator<std::string>(outStream, " "));
return outStream.str();
}
int main()
{
std::cout << reverse("Hello World") << "\n";
}
A common approach to do this is to reverse the entire string first, then for each word, reverse the letters in the word. So no recursion is necessary. You might find it easier to give this a try (yes, I know this isn't exactly an answer to your question :) ).
Use cout << str, not cout << *str to print a string. There's an operator<< overload for char *. But maybe that's not what you're trying to do; I can't quite follow your logic, in any event.
You're losing the "hello" part.
The algorithm you seem to go for does this:
each call to RevStr isolates the first word in the string it is passed as a parameter
calls RevStr with the remaining of the string
prints the word it isolated at step 1 as the stack unwinds
Basically, you should be printing the v1 data.
I would strongly advise making using some of the functionality exposed via std::string as a place to start.
One way you might do this would look like this:
std::string ReverseString(std::string s)
{
std::stack<std::string > stack;
std::string tmpstr = "";
std::string newstr = "";
size_t strsize = s.size();
size_t pos = 0; size_t tmppos = 0;
size_t i = 0; size_t stacksize = 0;
while( pos < strsize )
{
tmppos = s.find(" ", pos, 1); // starting as pos, look for " "
if (tmppos == std::string::npos) // std::string::npos => reached end
{
tmppos = strsize; // don't forget the last item.
}
tmpstr = s.substr(pos, tmppos-pos); // split the string.
stack.push(tmpstr); // push said string onto the stack
pos = tmppos+1;
}
stacksize = stack.size();
for ( i = 0; i < stacksize; i++ )
{
tmpstr = stack.top(); // grab string from top of the stack
stack.pop(); // stacks being LIFO, we're getting
if ( i != 0 ) // everything backwards.
{
newstr.append(" "); // add preceding whitespace.
}
newstr.append(tmpstr); // append word.
}
return newstr;
}
It's by no means the best or fastest way to achieve this; there are many other ways you could do it (Jerry Coffin mentions using std::vector with an iterator, for example), but as you have the power of C++ there, to me it would make sense to use it.
I've done it this way so you could use a different delimiter if you wanted to.
In case you're interested, you can now use this with:
int main(int argc, char** argv)
{
std::string s = "In Soviet Russia String Format You";
std::string t = ReverseString(s);
std::cout << t << std::endl;
}
given that its a char*, this reverses it inplace (ie, doesn't require more memory proportional to the incoming 'str'). This avoids converting it to a std::string ( not that its a bad idea to, just because it's a char* to start with.)
void reverse_words(char* str)
{
char* last = strlen(str) + str;
char *s, *e;
std::reverse(str,last);
for(s=e=str; e != last; e++)
{
if(*e == ' ')
{
std::reverse(s,e);
s = e+1;
}
}
std::reverse(s,e);
}
void Reverse(const string& text)
{
list<string> words;
string temp;
for ( auto cur = text.begin(); cur != text.end(); ++cur)
{
if (*cur == ' ')
{
words.push_front(temp);
temp.clear();
}
else
{
temp += *cur;
}
}
if (! temp.empty())
{
words.push_front(temp);
}
for_each(words.begin(), words.end(), [](const string& word) { cout << word << " "; });
cout << endl;
}
void swap(char* c1, char* c2) {
char tmp = *c1;
*c1 = *c2;
*c2 = tmp;
}
void reverse(char* s, char* e) {
if (s == NULL || e == NULL)
return;
while(s < e)
swap(s++, e--);
}
void reverse_words(char* line) {
if (line == NULL)
return;
reverse(line, line+strlen(line)-1);
char *s = line;
char *e;
while (*s != '\0') {
e = s;
while (*e != ' ' && *e != '\0') ++e;
--e;
reverse(s,e);
s = e+2;
}
}
I have the followoing
string myStr = "myname-abc";
string myStr1 = strstr(myStr.c_str(), "-");
now in myStr1 i have -abc. But i don't want "-" infront of it i want to have "abc" how do i do that using string data type.
Thanks for help.
Not sure if I'm following, but you want to erase the first element?
str.erase(0, 1); // erases 1 element starting from position 0
Also, if you just want to erase everything up to -:
str.erase(0, str.find('-') + 1);
If the data you are feeding the program isn't guaranteed to have a - somewhere, you should check the return value of str.find('-') for string::npos, the return value when no occurence is found.
string myStr1 = strstr(mStr.c_str(), "-") + 1;
or if you want to avoid converting to C style strings:
string myStr1(m_Str, m_Str.find('-') + 1);
size_t pos = myStr.find('-');
if (pos != std::string::npos)
{
myStr1 = myStr.substr(pos + 1);
}
This program outputs
abc abc
int main()
{
string myStr = "myname-abc";
string myStr1 = strstr(myStr.c_str(), "abc");
int index = 0;
while(myStr[index++] != '-'){}
string myStr2 = myStr.substr(index);
cout << myStr1 << " " << myStr2 << endl;
return 0;
}
std::string myStr("myname-abc");
std::string myStr1(++std::find(myStr.begin(), myStr.end(), '-'),
myStr.end());
If I understand what you are asking for, it's to remove instance of "-" from the string? If so, the following will also work..
string foo("-abc");
string::size_type fm = foo.find('-');
while (fm != string::npos)
{
foo.erase(foo.begin() + fm);
fm = foo.find('-', fm); // find the next instance of "-"
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char a[30];
cout<<"enter any string ";
cin.get(a,30);
char b[30];
for(int i=0;a[i]!='\0';i++)
{
if(a[i]!='!' && a[i]!='#'&& a[i]!='#' && a[i]!='$' && a[i]!='%' && a[i]!='^' && a[i]!='&' && a[i]!='*' && a[i]!='?')
{
b[i]=a[i];
cout<<b[i];
} }
getch();
}
The following program replaces all spaces with %20.the compilation works fine but the program terminates during the runtime.Any help???
#include<iostream>
#include<string>
using namespace std;
void removeSpaces(string url){
int len=url.length();
int i,count=0;
while(i<=len){
if(url[i]==' ')
count++;
i++;
}
int length2=len+(count*2);
string newarr[length2];
for(int j=len-1;j>=0;j--){
if(url[j]==' ')
{
newarr[length2-1]='0';
newarr[length2-2]='2';
newarr[length2-3]='%';
length2=length2-3;
}
else
{
newarr[length2-1]=url[j];
length2=length2-1;
}
}
cout<<"\nThe number of spaces in the url is:"<<count;
cout<<"\nThe replaced url is:"<<newarr;
}
int main(){
string url="http://www.ya h o o.com/";
removeSpaces(url);
}
This is called an "off by one" error.
while(i<=len){
if(url[i]==' ')
I'd also look at std::string::find() and std::string::replace() rather than what you're doing.
EDIT: Since the poster has said this isn't homework:
for (size_t pos = myString.find(' ');
pos != string::npos;
pos = myString.find(' ', pos))
{
myString.replace(pos, 1, "%20");
}
i is not initialized to 0 - this is the danger if using ',' instead of putting each variable on its own line.
As long as you're using string and not char *, why not use the string methods? This is essentially a translation of what you're trying to do (without even using ::find or ::replace):
void removeSpaces(string url)
{
string newUrl;
int count = 0;
for (int j = 0; j < url.length(); ++j)
{
if (url.at(j) == ' ')
{
newUrl.append("%20");
++count;
}
else
newUrl.append(url.at(j));
}
cout << "\nThe number of spaces in the url is:" << count;
cout << "\nThe replaced url is:"<< newUrl;
}
Edit: I see that #Bryan has given the version with ::find and ::replace.
string newarr[length2];
should be:
string newarr;
or
char newarr[length2];
or the more proper way:
char *newarr = new char[length2];
... // code.
delete[] newarr;
string replaceinString(std::string str, std::string tofind, std::string toreplace)
{
size_t position = 0;
for ( position = str.find(tofind); position != std::string::npos; position = str.find(tofind,position) )
{
str.replace(position ,1, toreplace);
}
return(str);
}
use it:
string replace = replaceinString(thisstring, " ", "%20");
string replace2 = replaceinString(thisstring, " ", "-");
string replace3 = replaceinString(thisstring, " ", "+");