How to reverse a substring of a string in c++? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to reverse a substring in a string. For example
string x = "foobar";
So I want to reverse foo to be oof so x would be "oofbar";
Here's a sample of what I have tried:
string x = "foobar";
size_t pos = x.find("foo");
reverse(x.begin() + k, s.end()); // I got stuck here.

For a simple case as
string x = "foobar";
you can use
std::reverse(x.begin(), x.begin() + 3); // reverse the first three letters.
If "foo" is embedded in the string, i.e. it is not at the start of the string, you'll have to find its location first.
string x = "whoknowswhereitisfoobar";
auto loc = x.find("foo");
if ( loc != std::string::npos )
{
std::reverse(x.begin() + loc, x.begin() + loc + 3);
}
If you don't want to hard code the number 3, you can use std::string::size():
string hay = "whoknowswhereitisfoobar";
string needle = "foo";
auto loc = hay.find(needle);
if ( loc != std::string::npos )
{
std::reverse(hay.begin() + loc, hay.begin() + loc + needle.size());
}
See Live Here

Given auto x = "foobar"s and const auto value = "foo"s, I'd probably use iterators just cause of their convenience:
const auto it = search(cbegin(x), cend(x), cbegin(value), cend(value));
if(it != cend(x)) {
x.replace(it, next(it, size(value)), crbegin(value), crend(value));
}
Live Example

Related

get substring regEx [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How can I get from string "C27_W112_V113_Table__6__1" string "6" or from string "C27_W120_V153_Table__22__1" string "22". thx
You can achieve it by two ways
Regex approach:
const re = /(\d+)__\d+$/;
let values = [
'C27_W112_V113_Table__6__1',
'C27_W120_V153_Table__22__1'
];
values.forEach(str => console.log(str.match(re)[1]));
string manipulation (I assume the value is always at the same place):
values.forEach(str => {
let reversed = str.split('').reverse().join('');
let index = reversed.indexOf('__');
console.log(reversed.slice(index+2, reversed.indexOf('__', index+1)));
});
Here a snippet
const re = /(\d+)__\d+$/;
let values = [
'C27_W112_V113_Table__6__1',
'C27_W120_V153_Table__22__1'
];
values.forEach(str => console.log(str.match(re)[1]));
console.log('--------');
values.forEach(str => {
let reversed = str.split('').reverse().join('');
let index = reversed.indexOf('__');
console.log(reversed.slice(index+2, reversed.indexOf('__', index+1)));
});

Split the end of a string in C++ using string delimiter (,) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have the below format of a string:
str1 = "a, b, c, d, e";
str2 = "aa, ba, ca, da, essd";
str2 = "aass, bsda, cads, dsda, esssdsd";
I want to extract the end of the string after splitting --> e, essd, esssdsd.
Assuming these are std::strings, I'd use rfind to find the last occurrence of the delimiter, and then take a substring from there. E.g.:
size_t index = str.rfind(", ");
string last_element = str.substr(index + 2); // 2 is the size of the delimiter

Extract int value and string from string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I would like to use string class, to extract some information from the string.
Given the string: <12,Apple>,<20,Orange>,<49,iPhone>
I would like to let 12, 20, 49 to a int array.
that means a[0] = 12, a[1] = 20, a[2] = 49.
And let Apple, Orange, iPhone to a String array.
that means b[0] = "Apple", b[1] = "Orange" b[2] = "iPhone"
How should I do?
Assume the string follows the format <int,string>,.... Please find the pseudo-code below:
Loop through the string `str` and
{
smaller_sign_pos = str.find('<', prev_pos)
entry_comma_pos = str.find(',', smaller_sign_pos+1)
greater_sign_pos = str.find('>', entry_comma_pos+1)
if (all pos values are not `npos`)
{
int_value = atoi(str.substr(smaller_sign_pos+1, entry_comma_pos-smaller_sign_pos-1))
str_value = str.substr(entry_comma_pos+1, greater_sign_pos-entry_comma_pos-1)
prev_pos = greater_sign_pos+1
append int_value to int array
append str_value to string array
optional: you can check if the comma after '>' exists
}
else
{
break or set the end of loop flag
}
}

C++ find whitespaces in substring [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to know how can I find empty or whitespaces within substring in C++. For example:
string str = "( )"; // or str = "()"
Here, I want to make sure there is always something in between parenthesis. Function isspace() takes only one character so I have to search in loop. Is there any better way to do this? Thanks for your help.
You can use std::string::find() to find the ( and ) characters, then use std::string::find_first_not_of() to check for any non-whitespace characters between those indexes.
string str = "( )"; // or str = "()"
string::size_type idx1 = str.find("(");
if (idx1 != string::npos) {
++idx1;
string::size_type idx2 = str.find(")", idx1);
if (idx2 != string::npos) {
string tmp = str.substr(idx, idx2-idx1);
string::size_type idx3 = tmp.find_first_not_of(" \t\r\n");
if (idx3 != string::npos) {
...
}
}
}

Split multiple Words in a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a String like this
String s1="1 AND 1 OR 1";
I want split with AND OR and my result should be 1,1,1
I am using JAVA /eclipse
package com.test;
String s1="1 AND 1 OR 1";
String[] splits=s1.split("[AND\OR]");
for (int i = 0; i < splits.length; i++) {
System.out.println(splits[i]);
}
}
}
Can I get any help how to do this?
Any Help appreciated
I am unsure about the language, but for C# you can use the following:
string s1 = "1 AND 1 OR 1";
string s2 = s1.Replace("AND", ",").Replace("OR", ",");
Console.WriteLine(s2);
Which doesn't use regular expressions.
If you want an array, you can use the following:
string s1 = "1 AND 1 OR 1";
string[] s2 = Regex.Split(s1.Replace(" ", string.Empty), "AND|OR");
In Java you can replace using the same mechanism:
String s1 = "1 AND 1 OR 1";
String s2 = s1.replace("AND", ",").replace("OR", ",");
System.out.println(s2);
And to get an array:
String s1="1 AND 1 OR 1";
String[] s2 = s1.replace(" ", "").split("AND|OR");