cpp Get numbers from string - c++

What is the simple way to get numbers from given string pattern using regular expressions?
the string pattern is like,
${type:1234} ${type:2345}
I want the numbers, in that case, 1234, 2345.
the string patten can also contain spaces,
${(WS)*type(WS)*:(WS)*1234(WS)*} , ... (more like this)
I need also to check that the string is valid pattern and if it is, to extract the numbers.
I know it can be easily done using tokenizer but I think it will be better to use regular expressions.

you use some magic to achieve what you want using loops:
#include <iostream>
#include <string>
int main()
{
std::string str("${type:1234} ${type:2345}");
int n = 0;
for(int i(0); i < str.length(); i++)
{
if(isdigit(str[i]))
{
n++;
while(isdigit(str[i]))
i++;
}
}
std::cout << "There are: " << n << std::endl;
std::string* strTmp = new std::string[n];
int j = 0;
for(int i = 0; i < str.length(); i++)
{
if(isdigit(str[i]))
{
while(isdigit(str[i]))
{
strTmp[j] += str[i];
i++;
}
j++;
}
}
for(int i = 0; i < n; i++)
std::cout << strTmp[i] << std::endl;
// now you have strTmo holding numbers as strings you can convert them to integer:
int *pInt = new int[n];
for(int i = 0; i < n; i++)
pInt[i] = atoi(strTmp[i].c_str());
for(int i = 0; i < n; i++)
std::cout << "value " << i+1 << ": " << pInt[i] << std::endl;
delete[] strTmp;
strTmp = NULL;
delete[] pInt;
pInt = NULL;
std::cout << std::endl;
return 0;
}

Related

No operator == matches these operands

int main() {
int x;
const int Maxword = 5;
std::string Guess[Maxword];
std::string words[Maxword] = {
"Hello",
"World",
"Shift",
"Green",
"Seven"
};
srand(time(NULL));
int iSecret = rand() % 4 + 1;
std::string Cool(words[iSecret]);
for (int i = 0; i < 5; i++) {
std::cout << Cool[i] << std::endl;
}
for (int i = 0; i < 5; i++) {
std::cout << ("Please enter the letters you would like to guess") << std::endl;
std::cin >> Guess[i];
std::cout << Guess[i] << std::endl;
}
for (int i = 0; i < 5; i++) {
if (Guess[i] == Cool[i]) {
std::cout << Guess[i] << "Is in the word";
}
}
For this statement here at the Bottom for statement within the if statement it has a no operator dont mind the actual code it is just a rough draft before I make the actual code but i dont see the problem.
Cool is a string. Cool[i] is a character. Guess is an array of strings. Guess[i] is a string. You're trying to compare a character to a string. You probably mean if (guess[i] == Cool)

I am writing a code to store an int array in a file and then retrieve it in c++, but on retrieving the first item is bogus value, how do i fix this?

I am trying to store integer array in a file in c++.
the code is
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
int arr[5];
for (int i = 0; i < 5; i++) {
arr[i] = i;
cout << arr[i];
}
cout << endl;
ofstream out;
out.open("arr.txt");
for (int i = 0; i < 5; i++) {
out << arr[i];
}
out.close();
ifstream in;
in.open("arr.txt");
for (int i = 0; i < 5; i++) {
in>>arr[i];
}
in.close();
for (int i = 0; i < 5; i++) {
cout << i << " " << arr[i] << endl;
}
cout << arr[0] << endl;
return 0;
}
Now the output i am getting is
01234
1234
1
2
3
4
where is the zero in the first index?
When you write the data, you're going to need to put whitespace between the numbers
for (int i = 0; i < 5; i++) {
out << arr[i] << ' ';
}
Otherwise they'll be read in as a single int all at once.
You forgot to add spaces to your output. When you do
for (int i = 0; i < 5; i++) {
out << arr[i];
}
arr contains {0,1,2,3,4} so you write to the file 01234 and when you read that back with
for (int i = 0; i < 5; i++) {
in>>arr[i];
}
The whole 01234 is tread as a single integer of the value 1234 instead of reading each digit as a seperate number. Changing your output loop to
for (int i = 0; i < 5; i++) {
out << arr[i] << " ";
}
will make the file have 0 1 2 3 4 and then those will be read in as separate integers.

Vectors And Merging

For this vectors and merging assignment, we are supposed to read in user inputted strings and sort them alphabetically. I got the first two parts, but when I am putting the sorted elements in the new vector, it says that my new vector is out of range. Does anyone know how to fix this?
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> que1;
vector<string> que2;
vector<string> que_merge;
string firstName;
string secondName;
int counterq1 = 0;
int counterq2 = 0;
cout << "Enter queues: " << endl;
bool check = true;
while(check) {
cin >> firstName;
if (firstName == "ENDQ"){
check = false;
}
else{
que1.push_back(firstName);
counterq1++;
check = true;
}
}
// que1.resize(counterq1);
bool check2 = true;
while (check2) {
cin >> secondName;
if (secondName == "ENDQ") {
check2 = false;
} else {
que2.push_back(secondName);
counterq2++;
}
}
// que2.resize(counterq2);
cout << "que1: " << counterq1 << endl;
for (int i = 0; i < que1.size(); i++) {
cout << que1.at(i) << endl;
}
cout << endl;
cout << "que2: " << counterq2 << endl;
for (int j = 0; j < que2.size(); j++) {
cout << que2.at(j) << endl;
}
cout << endl;
cout << "que_merge: " << counterq1 + counterq2 << endl;
int i = 0;
int k = 0;
int j = 0;
while (1){
if (i >= counterq1 || j >= counterq2){
break;
}
if(que1.at(i) < que2.at(j)){
que_merge.push_back(que1.at(i));
i++;
}
else{
que_merge.push_back(que2.at(j));
j++;
}
k++;
}
if (que1.empty()){
for (int m = j; m < counterq2; m++){
que_merge.push_back(que2.at(m));
}
} else {
for (int l = i; l < counterq1; ++l) {
que_merge.push_back(que1.at(l));
}
}
for (int l = 0; l < (counterq1+counterq2); l++) {
cout << que_merge.at(l) << endl;
}
return 0;
}
I think your problem is that these lines:
if (que1.empty()){
for (int m = j; m < counterq2; m++){
que_merge.push_back(que2.at(m));
}
} else {
for (int l = i; l < counterq1; ++l) {
que_merge.push_back(que1.at(l));
}
}
doesn't do what you expect.
As far as I can see, your idea is to merge the remaining element from either que1 or que2.
However, that is not what you get from this code as the elements in que1 and que2 is never erased. In other words - the check for an empty queue is rather meaningless and you can't be sure that all elements are added to que_merge
So when you do:
for (int l = 0; l < (counterq1+counterq2); l++) {
cout << que_merge.at(l) << endl;
}
you may read beyond the number of elements in que_merge
Tip:
Don't count the number of elements your self. Use size() (like que_merge.size()) instead. For instance:
for (int l = 0; l < que_merge.size(); l++) {
cout << que_merge.at(l) << endl;
}
or a range based loop like:
for (const auto& s : que_merge) {
cout << s << endl;
}
#include <algorithm>
Use std::merge and std::sort, not necessarily in that order.
If the assignment says you can't do it the C++ way, that you have to write it all out, you can do it the engineer's way: Find an example that works, and crib it.
There are two possible implementations of std::merge on this page: All about C++ merge

Replace characters in old array

I'm solving a problem and stuck on last part now what i am doing. Taking 5 characters from user and save it on character array and than saying enter 3 characters to check does array has your enter characters in it.
For example: User enter 5 characters dagpl.Than second array subArray which search characters from main array now user enter 3 charactersdgl.Result saying 3 characters found. Are you want to replace these 3 characters with new characters? So enter 3 new replace characters now user enter xyz.
Final array would be replace like this xaypz.
My Code doesn't working fine for replacing characters i don't know what i'm doing wrong.
#include<iostream>
#include<cstdlib>
using namespace std;
int main(int argc, char**argv) {
bool check = false;
char arr[6] = { '\0' };
char subarr[4] = { '\0' };
int count = 0;
cout << "Enter Characters : ";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout << "Enter 3 Characters and see how many times does array has your Search Characters : ";
for (int i = 0; i < 3; i++) {
cin >> subarr[i];
}
//Sub Array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
if (subarr[i] == arr[j]) {
if (!check) {
cout << "Found characters are: ";
}
count++;
cout << subarr[i] << ",";
check = true;
}
}
}
if (check) {
cout << '\b';
cout << " ";
cout << endl;
}
if (!check) {
cout << "Sorry Nothing Found" << endl;
}
cout << "total Found : " << count << endl;
//SECTION 3
if (check) {
int n = count + 1;
char* replace = new char[n]();
cout << "You can only replace " << count << " new characters because of find operation! so enter it will be replace old array with it: ";
for (int i = 0; i < n - 1; i++) {
cin >> replace[i];
}
//Replace characters
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < 5; j++) {
if (subarr[i] == arr[j]) {
arr[j] = replace[j];
}
}
}
delete[]replace;
replace = NULL;
cout << "New Array would be: ";
for (int i = 0; i < 5; i++) {
cout << arr[i];
}
cout << endl;
}
system("pause");
return EXIT_SUCCESS;
}
You're not marking the matched characters from arr
Replace the matched characters from arr[j] with a distinct character so that you can determine where to replace later. you can use null terminator
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
if (subarr[i] == arr[j]) {
if (!check) {
cout << "Found characters are: ";
}
count++;
cout << subarr[i] << ",";
check = true;
arr[j]='\0'; //replacing the matched character with null terminator
}
}
}
Now traverse through your arr and replace null terminators with characters from replace array
int i=0;
for (int j = 0; j < 5; j++) {
if (arr[j]=='\0') { //replace when it equals to null terminator
arr[j] = replace[i++];
}
}
You want to ask the user to replace a specific number of chars, counting how many chars appear in subarr.
arr is input, subarr is from:
int replacecount[3] = { 0 };
for (int x=0; x<6; x++)
for (int i=0; i<3; i++)
if (input[x] == from[i]) {
++replacecount[i];
break;
}
int count = 0;
for (int i=0; i<3; i++)
if (replacecount[i] > 0)
++count;
std::cout << " you can replace " << count << " characters..." << std::endl;
char to[3];
for (int i=0; i<3; i++)
if (replacecount[i] > 0) {
std::cout << "enter replace character for '" << from[i] << "'" << std::endl;
std::cin >> to[i];
}
for (int x=0; x<6; x++)
for (int i=0; i<3; i++)
if (input[x] == from[i]) {
input[x] = to[i];
break;
}
std::cout << "replaced string is: " << input << std::endl;

Can't return dynamic string from a function

I was writing some code challenge from reddit about encrypting strings and I came up with something like this:
#include <iostream>
#include <string>
using namespace std;
string encrypt(string sentence);
int main()
{
string sentence;
int i = 0;
cout << "Welcome. Enter a sentence: ";
getline(cin, sentence);
cout << sentence << endl;
encrypt(sentence);
cout << endl << endl;
system("pause");
return 0;
}
string encrypt(string sentence)
{
int i = 0;
int x = (sentence.size());
string *encrypted_sentence = new string[sentence.size()];
int *wsk = new int[sentence.size()];
for (i = 0; i < x; i++)
{
wsk[i] = sentence[i];
}
for (i = 0; i < x; i++)
{
if (wsk[i] == ' ')
continue;
else if (islower(wsk[i]))
{
if (wsk[i] <= 99)
wsk[i] = (wsk[i] + 23);
else
wsk[i] = (wsk[i] - 3);
}
else
{
if (wsk[i] <= 67)
wsk[i] = (wsk[i] + 23);
else
wsk[i] = (wsk[i] - 3);
}
}
for (i = 0; i < x; i++)
{
//cout << static_cast <char> (wsk[i]);
encrypted_sentence[i] = wsk[i];
}
return *encrypted_sentence;
}
My problem is, that there is nothing that gets returned. After I run the program I get nothing in return. Can anybody point me in the right direction with this? What have I missed?
First main() returns an int always. void main() is not standard. Secondly:
string encrypted_sentence = new string[sentence.size()];
Will not even compile. encrypted_sentence is of type std::string but you are trying to assign to it a std::string *. Third you should avoid using using namespace std;
Update:
I believe you are trying to output the encrypted string at:
cout << endl << endl;
But all this is doing is outputting 2 newlines and flushing the output twice. If you want to display the encrypted string then you either need to capture the return of the encrypt() function and display it or encrypt() can take the string in by reference. IF you change encrypt() to take a reference then it would become:
void encrypt(string & sentence)
{
string *encrypted_sentence = new string[sentence.size()]; // get rid of this line as it is not needed.
//...
for (i = 0; i < x; i++)
{
sentence[i] = wsk[i];
}
}
And then you would output the string with:
cout << sentence << endl;
In case anyone would seek an answer to this question, I've come up with this, and I'm pretty sure it finally works how I wanted it to:
#include <iostream>
#include <string>
std::string encrypt(std::string to_encrypt);
int main()
{
std::string sentence;
std::string result;
std::cout << "Welcome. Please enter a sentence: ";
getline(std::cin, sentence);
result = encrypt(sentence);
std::cout << "Result: " << result << std::endl;
system("pause");
return 0;
}
std::string encrypt(std::string to_encrypt)
{
int i = 0;
int x = (to_encrypt.size());
std::cout << std::endl << "x = " << x << std::endl;
int *temp = new int[to_encrypt.size()];
for (i = 0; i < x; i++)
{
temp[i] = to_encrypt[i];
}
for (i=0; i < x; i++)
{
if (temp[i] == ' ')
continue;
else if (islower(temp[i]))
{
if (temp[i] <= 99)
temp[i] = temp[i] + 23;
else
temp[i] = temp[i] - 3;
}
else
{
if (temp[i] <= 67)
temp[i] = temp[i] + 23;
else
temp[i] = temp[i] - 3;
}
}
std::string encrypted;
for (i = 0; i < x; i++)
{
encrypted += (static_cast <char> (temp[i]));
}
return encrypted;
}
The code is obviously wrong. string *encrypted_sentence = new string[sentence.size()]; allocates an ARRAY of strings! Not a single string. Judging from that, you can see how your code is wrong.