String manipulation C++ - c++

I need to make some functions but i don't know how.
join two strings, input positions and output the characters from the position in the string
Example: s1=first, s2=second; s1+s2=firstsecond, pos=3,7,10, output=ren
this is my code for one postion, but i don't know how to make for several position, main problem is how to limit input of position:
s=s1+s2;
cin>>pos;
cout<<s[pos-1];
define vowel in string and replace that vowel with string
Example: s=firstsecondthird, vowel=i, str=EXA, output=fEXArstsecondthEXArd
This is what i know, i don't know how to make the replacing vowel with string(str)
cin>>vowel;
if(check is defined character vowel)
{
cin>>str;
.
.
.
}
Thank you

Catch! :)
#include <iostream>
#include <string>
#include <cstring>
int main()
{
std::cout << "Enter first string: ";
std::string s1;
std::cin >> s1;
std::cout << "Enter second string: ";
std::string s2;
std::cin >> s2;
s1 += s2;
std::cout << "The joined string is " << s1 << std::endl;
std::cout << "Enter several positions in the joined string (0-stop): ";
std::string s3;
std::string::size_type pos;
while ( std::cin >> pos && pos != 0 )
{
if ( --pos < s1.size() ) s3 += s1[pos];
}
std::cout << "You have selected the following letters " << s3 << std::endl;
const char *vowels = "aeiou";
char c;
do
{
c = '\0';
std::cout << "Enter a vowel: ";
} while ( std::cin >> c && !std::strchr( vowels, c ) );
if ( c != '\0' )
{
for ( auto pos = s1.find( c, 0 );
pos != std::string::npos;
pos = s1.find( c, pos ) )
{
const char *t = "EXA";
const size_t n = 3;
s1.replace( pos, 1, t );
pos += n;
}
std::cout << "Now the joined string looks like " << s1 << std::endl;
}
return 0;
}
If to enter
first
second
3 7 10 0
i
then the program output will be
Enter first string: first
Enter second string: second
The joined string is firstsecond
Enter several positions in the joined string (0-stop): 3 7 10 0
You have selected the following letters ren
Enter a vowel: i
Now the joined string looks like fEXArstsecond
You can use it as a template for your great program.:)

Related

C++ how to split string with alphabets and numbers

I have a need to split the following string into their corresponding alpahbets and numbers
CH1000003
ABC000123
WXYZ10001
Results I want are
st1: CH
st2: 1000003
st1: ABC
st2: 000123
st1: WXYZ
st2: 10001
Now I do have a working code but the amount of code I have written seems a bit too much. There has to be an easy way. Perhaps somehow use regex in C++? Suggestions?
My code:
std::string idToCheckStr="CH1000003";
//find length of string
int strLength = idToCheckStr.length();
cout << "idToCheckStr: " << idToCheckStr <<endl;
cout << "strLength : " << strLength <<endl;
string::iterator it;
int index = 0;
for ( it = idToCheckStr.begin() ; it < idToCheckStr.end(); it++ ,index++)
{
//check where the numbers start in the string
if (std::isdigit(*it) != 0)
{
cout<< "FOUND NUMBER!" <<endl;
cout<< index << ": " << *it <<endl;
break;
}
cout<< index << ": " << *it <<endl;
}
std::string firstPartStr = idToCheckStr.substr (0,index);
cout << "firstPartStr: " << firstPartStr <<endl;
std::string secondPartStr = idToCheckStr.substr (index,strLength);
cout << "secondPartStr: " << secondPartStr <<endl;
OUTPUT:
idToCheckStr: CH1000003
strLength : 9
0: C
1: H
FOUND NUMBER!
2: 1
firstPartStr: CH
secondPartStr: 1000003
Thanks to igor.
size_t first_digit = idToCheckStr.find_first_of("0123456789");
cout << "first_digit: " << first_digit <<endl;
std::string str1 = idToCheckStr.substr (0,first_digit);
cout << "str1: " << str1 <<endl;
std::string str2 = idToCheckStr.substr (first_digit,idToCheckStr.length());
cout << "str2: " << str2 <<endl;
OUTPUT:
first_digit: 2
str1: CH
str2: 1000003
Here is one of the simple ways to handle your problem.
I find this more understandable for you.
string s = "CH1000003";
// cin >> s; if you waant to read the input
string st1 = "", st2 = "";
for(auto ch : s) {
if(isdigit(ch)) st2 += ch;
else if(isalpha(ch)) st1 += ch;
else {} // if you want something else
}
cout << "st1: " << st1 << endl;
cout << "st2: " << st2 << endl;
You could indeed use regular expressions for this:
The pattern would be ([A-Z]+)([0-9]+), i.e. any combination of 1 or more uppercase letters followed by any combination of 1 or more numbers. The parentheses allow you to capture those 2 groups to be able to access them later on.
std::regex_match(line, matches, pattern) takes an input line, and tries to match it against a pattern. If it can, stores the matches in a std::smatch array; where the first entry is always the whole match, and the successive ones are for each capturing group. If it can't, it just returns false.
Should you need to relax the regular expression, e.g. allowing white spaces before, after, or in the middle of the input string, you could do it easily just changing the pattern: \s*([A-Z]+)\s*([0-9]+)\s*.
[Demo]
#include <fmt/core.h>
#include <iostream> // cout
#include <regex> // regex_match, smatch
#include <string>
int main() {
std::string line{};
std::regex pattern{R"(([A-Z]+)([0-9]+))"};
while (std::getline(std::cin, line)) {
std::smatch matches{};
if (std::regex_match(line, matches, pattern)) {
std::cout << fmt::format("line = '{}', alphabets = '{}', numbers = '{}'\n",
matches[0].str(), matches[1].str(), matches[2].str());
}
}
}
// Outputs:
//
// line = 'CH1000003', alphabets = 'CH', numbers = '1000003'
// line = 'ABC000123', alphabets = 'ABC', numbers = '000123'
// line = 'WXYZ10001', alphabets = 'WXYZ', numbers = '10001'

Recursion to remove duplicate characters c++

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void rmvdupli(string s)
{
if (s.length() == 1)
{
cout << s;
return;
}
char c = s.at(0);
if (((s.substr(1)).find(c)) >= 0)
cout << "";
else
cout << c;
rmvdupli(s.substr(1));
}
int main()
{
cout << "Enter the string " << endl;
string s;
cin >> s;
rmvdupli(s);
return 0;
}
Output
Enter the string
ababcdc
c
What is the problem with the code? It seems perfectly alright but answer is not coming!!
I modified your code with a bit of refactoring in order to fix the bugs and improve its readability and performance. I also added another overloaded function that is significantly faster and uses 0 heap allocations. It does the same thing though but in a far more optimized way.
Here:
#include <iostream>
#include <string>
#include <string_view>
// this is basically your function but is more efficient now
void removeDuplicateChars( const std::string& str )
{
if ( str.length( ) == 1 )
{
std::cout << str;
return;
}
const char firstChar = str[ 0 ];
const std::string&& restOfStr { str.substr( 1 ) };
if ( restOfStr.find( firstChar ) == std::string::npos )
{
std::cout << firstChar;
}
else
{
std::cout << "";
}
removeDuplicateChars( restOfStr );
}
// this one is far superior to the above one
void removeDuplicateChars( std::string_view&& strView )
{
if ( strView.length( ) == 1 )
{
std::cout << strView;
return;
}
const char firstChar = strView[ 0 ];
strView.remove_prefix( 1 );
if ( strView.find( firstChar ) == std::string_view::npos )
{
std::cout << firstChar;
}
else
{
std::cout << "";
}
removeDuplicateChars( std::move( strView ) );
}
int main( )
{
std::cout << "Enter the string: ";
std::string str;
std::cin >> str;
removeDuplicateChars( str ); // this calls the first overload
std::cout << '\n';
removeDuplicateChars( std::string_view( "ababcdc" ) ); // this calls the
// second overload
return 0;
}
Sample input/output:
Enter the string: ababcdc
abdc
abdc
I would highly recommend you to ditch the 1st overload and only use the 2nd one. Not only it's faster but also supports the types std::string, std::string_view, and C-style strings.
Here are some useful links to improve your knowledge:
std::string_view
std::string::substr
std::string::find
The below line:
if (((s.substr(1)).find(c)) >= 0)
should be changed to:
if (s.substr(1).find(c) != std::string::npos)
It basically means that c is found in s.substr(1).
find returns std::string::npos if the character is not found.
I have tried to solve this problem of yours.
What I have done is, I have replaced duplicate characters with whitespace.
if you want to remove whitespace you can find the code easily on internet.
C++ Code:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void RemoveDuplicateChar(string inputString)
{
if(inputString.length() == 1){
cout << inputString;
cout << "\nIt has only one character";
}else{
int stringLength = inputString.length();
cout<< "Original string: ";
cout << inputString;
cout << "\n";
for(int i =0 ; i<stringLength; i++){
for(int j = i+1; j< stringLength; j++){
if(inputString[i] == inputString[j]){
inputString[j] = ' ';
}else{
continue;
}
}
}
cout << "Duplicate character removed\n";
cout << "New String: ";
cout << inputString;
}
}
int main()
{
string input;
cout << "Enter a string with repeated characters: ";
cin >> input;
RemoveDuplicateChar(input);
return 0;
}
Output:
Enter a string with repeated characters: ababcdc
Original string: ababcdc
Duplicate characters removed
New String: ab cd

Find a word in a sentence c++

This code should say if a word is present in a sentence or not. When I insert the sentence and the word where I declare the strings(for exemple: string s = "the cat is on the table" string p = "table" the program says that the word is in the sentence) the code works but, with the getline, the for cycle never begin and it always says that the word isn't in the sentence.
Please help I dont know what to do
#include <iostream>
#include <string>
using namespace std;
int main () {
string s;
string p;
string word;
bool found = false;
int sl = s.length();
int beg = 0;
int pl = p.length();
cout << "sentence: ";
getline(cin, s);
cout << "word: ";
getline(cin, p);
for(int a = 0; a<sl; a++)
{
if(s[a]== ' ')
{
word = s.substr(beg, a-beg);
if (word== p)
{
found = true;
break;
}
beg = a+1;
}
}
if (found== true)
{
cout <<"word " << p << " is in a sentence " << s;
}
else
{
word = s.substr(beg);
if (word== p)
{
found = true;
}
if(found == true)
{
cout <<"the word " << p << " is in the sentence " << s;
}
else
{
cout <<"the word " << p << " isn't in the sentence " << s;
}
}
}
after taking the input strings then use length() to find the length, otherwise you are not taking the actual size of the strings.
getline(cin, s);
getline(cin, p);
int sl = s.length();
int pl = p.length();
For splitting the words after taking the input string by getline() you can use stringstream which is a builtin c++ function, like :
#include <sstream>
#include <iostream>
using namespace std;
int main(){
string arr;
getline(cin, arr);
stringstream ss(arr);
string word;
while(ss >> word){
// your desired strings are in `word` one by one
cout << word << "\n";
}
}
Another thing is that you can declare the strings like string s, p, word;

how to initialize the recursive function for length

int countChars(string str)
{
int count = 0;
if (str == "")
return count;
else
{
count++;// add a character to the count
return count + countChars(str.substr(1));// function calls itself
}
}
I need to take the above function and call in in the program below and I'm not sure how to initialize it properly. Below is what I tried and it doesn't work. I'm not allowed to use the .length() because otherwise the program would be done.
int main()
{
char find = '\0';
string str;
int count = 0;
int length = int(countChars);
//ask the user for a sentence
cout << "Enter a sentence " << endl;
getline(cin, str);
//ask the user which letter they want the count of
cout << "Which letter would you like to find the number of appearances: " << endl;
cin >> find;
for (int i = 0; i < length; i++)
{
if (str[i] == find)
{
count++;
}
}
cout << "the letter " << find << " appears " << length << " times " << endl;
//waits for user to exit
system("pause");
cin.get();
}
It seems the function should count the number of appearances of a letter in a string. If so then it is declared and defined incorrectly. It has to have at least two parameters an object of type std::string and an object of type char.
Here is shown how such a recursive function can look
#include <iostream>
#include <string>
size_t countChars( const std::string &s, char c )
{
return s.empty() ? 0 : ( s[0] == c ) + countChars( { s, 1 }, c );
}
int main()
{
std::cout << "Enter a sentence ";
std::string s;
std::getline( std::cin, s );
std::cout << "Which letter would you like to find the number of appearances: ";
char c = '\0';
std::cin >> c;
std::cout << "The letter " << c
<< " appears " << countChars( s, c )
<< " times " << std::endl;
return 0;
}
The program output might look like
Enter a sentence My name is Alycia
Which letter would you like to find the number of appearances: a
The letter a appears 2 times
If you mean a function that just calculates the length of a string then it can look like
size_t countChars( const std::string &s )
{
return s.empty() ? 0 : 1 + countChars( { s, 1 } );
}
and shall be called after the statement
getline(cin, str);

How do I replace a string with another string?

I've sat on this problem for quite some time and I can't figure out, what to do.
I am trying to write a programm, that reads a text file, searches and replaces string and saves the file under a new name. Depending on your input with minimum and maximum value as well as inkrement, several files are created.
Everything works except the replacing of a string (function replaceVariable).
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <conio.h>
using namespace std;
string replaceVariable(string text1, string oldVariable, long double wert){
cout<< "replace-function open............."<<endl;
size_t foundAt = text1.find(oldVariable); //find position of old variable
cout<<"position old variable: "<<foundAt<<endl;
string newText = to_string(wert); //convert long double 'wert' to string
cout<<"new variable: "<<newText<<endl;
size_t lengthNewText = newText.length(); //find length of new string
string text2= text1.replace(foundAt, lengthNewText, newText); //replace with new string with length 'lengthNewText' starting at position 'foundAt'
return text2;
}
void writeFile ( string text, string filename ){
ofstream myfile;
myfile.open ( filename.c_str() );
myfile << text;
cout<<"file written.............."<<endl;
myfile.close();
}
template <typename T>
std::string to_string(T const& value) {
stringstream sstr;
sstr << value;
return sstr.str();
}
int main(){
ifstream inFile;
inFile.open("C:\\Users\\User\\Desktop\\Test\\testing.txt");//open the input file
if (inFile.is_open()){
cout<< "file open"<<endl<<endl;
stringstream strStream;
strStream << inFile.rdbuf();
string str = strStream.str();
cout << "---------------------------------------------------------------"<<endl;
cout<< str << endl;
cout << "---------------------------------------------------------------"<<endl<<endl;
string line;
string name;
long double minWert = 0;
long double maxWert = 0;
long double inkWert = 0;
cout << "Enter minimum value:" << endl;
cin >> minWert;
cout << "Enter maximum value:" << endl;
cin >> maxWert;
cout << "Enter inkrement:" << endl;
cin >> inkWert;
int numFiles = (maxWert-minWert)/inkWert + 1; //calculation number of files needed
cout << "minimum value: " << minWert << endl;
cout << "maximum value: " << maxWert << endl;
cout << "inkrement: " << inkWert << endl;
cout << "number of files: " << numFiles << endl<<endl<<endl;
string oldVariable = "xyz "; //string to be replaced, xyz followed by 5 spaces
for( int fileNum = 1; fileNum <= numFiles; ++fileNum ) {
cout<< "loop number: "<< fileNum<<endl;
string output = str;
replaceVariable(output, oldVariable, minWert);
cout << "---------------------------------------------------------------"<<endl;
cout << output << endl;
cout << "---------------------------------------------------------------"<<endl<<endl;
string text = output;
name = "C:\\Users\\User\\Desktop\\Test\\comp";
name += to_string( fileNum );
name += ".bdf";
writeFile( text, name );
cout<<minWert<<endl;
minWert = minWert+inkWert;
cout <<"new Minimalwert: "<< minWert<<endl<<endl;
}
inFile.close();
} else{cout << "Unable to open file";}
getch();
return 0;
}
I've already searched numerous sites and googled every thinkable combination.
Do you have any ideas what might help?
If your functionality of the function 'replaceVariable' is right then this might be the issue,
string output = str;
/*function replaceVariable is returning replaced string but you didn't receive
at the calling place and assign back to output(which you are writing in output file)*/
replaceVariable(output, oldVariable, minWert);
So replace like,
string output = replaceVariable(str, oldVariable, minWert);
Several points to be noted in your code.
First, string::find may return npos if the pattern string is not found, you should check that.
Second, string::replace does an inplace replacement on the original string, for better performance you could pass the text1 argument by reference.
Third, replaceVariable only replaces the first occurrence of the variable, is that really what you want?
Here is my version of replacing patterns in a string:
// replaces at most `limit` occurrences of pattern `p` in text `s` with string `repl`.
// if `limit` <= 0, replace all occurrences.
// returns number of replacement that actually took place.
int replace(std::string &s, const std::string &p, const std::string &repl, int limit=0) {
int nrepl = 0;
size_t pos = 0,
plen = p.length(),
rlen = repl.length(),
npos = std::string::npos;
while ((pos = s.find(p, pos)) != npos) {
s.replace(pos, plen, repl);
pos += rlen;
++nrepl;
if (limit > 0 && nrepl >= limit) break;
}
return nrepl;
}
Hope that helps.