I am trying to substring some expressions into individual tokens such as !, &, | (), etc. What I am having trouble with is the fact that when I try to make a sub-string of "!(S&B|H)&!(S&J|R)&!(P)" with the cout line below, I get: "(S&J|R)&!(P)", when I thought it should be: "(S&J|R)". It either is beyond what I have seen or just so simple that I just am not getting it. Any help would help a lot. Thanks.
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char * argv[]) {
string name = "!(S&B|H)&!(S&J|R)&!(P)";
cout<<name.substr(10,16)<<endl;
return 0;
}//Main
I did not understand your question well but if you want to get
(S&J|R)
You should do:
name.substr(10,7)
The second parameter is the length.
Related
Currently going thru a c++ course.
I had to create a word cipher using the strings: alphabet and key.
to cipher an inputted word with less code as possible I created this solution that gives the error:
no matching function for call to std::basic_string<char>::find(std::string&, int&, int)
I don't know how to solve it, neither do I know if my idea would work at all, would LOVE some help.
Thanks for your attention :)
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
string word_to_encrypt {};
getline (cin,word_to_encrypt);
for (int i=0;i<word_to_encrypt.size;i++){
word_to_encrypt.replace (i,1,key,(alphabet.find(word_to_encrypt,i,1)),1);
}
cout<< word_to_encrypt;
}
Two problems:
First size is a function and not a variable. Therefore you need size().
Secondly std::string::find() has no overload which takes a std::string and two ints: https://en.cppreference.com/w/cpp/string/basic_string/find , but you can use the overload which takes a CharT instead by adding .c_str() or .data().
This compiles at least:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
string word_to_encrypt {};
getline (cin,word_to_encrypt);
for (int i=0;i<word_to_encrypt.size();i++){
word_to_encrypt.replace(i, 1, key, (
alphabet.find(word_to_encrypt.c_str(), i, 1)),1);
}
cout<< word_to_encrypt;
}
Check if a string is palindrome
I was using the link above to try to solve this problem (among many others, Ive been trying to solve it various ways all day with no dice, this is my first C++). All other examples are usually in an array format, and I can't make assumptions as to the length of a word.
I'm trying to make a program to detect if a word is a palindrome or not. I have a text file with one word per line, and want to test each word, line by line, if it is a palindrome, and if so to print it to the screen, and if not, to ignore it and not print it.
I figured the best way to locate the palindromes was to reverse the word line by line and match it to the original, and if they are the same (==), then to print it. Here is what I have so far:
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
std::string line;
std::ifstream infile("wordlist.txt");
}
string reverse(string line){
if (line == string(line.rbegin(), line.rend())) {
cout << string;
}
}
All help is appreciated
I guess your question is a homework question and you would like to get some information on how to complete the C++ coding.
You look not to know how to read file contents in C++.
Here's a link of how to do it:
Read file-contents into a string in C++
I am not very sure about what you specifically would like to be answered. If your question is a homework question, here's some info of how to ask:
https://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions
#include<iostream>
#include<algorithm>
#include<string.h>
#include<fstream>
using namespace std;
int main() {
string line="", line_rev="";
ifstream infile;
infile.open("wordlist.txt");
do{
infile>>line;
line_rev=line;
reverse(line_rev.begin(), line_rev.end());
if(line==line_rev)
cout<<line<<endl;
}while(getline(infile, line));
//if(infile.is_open()){cout<<"open"<<endl;} //to check if file is open or not
//else{cout<<"unable to open"<<endl;}
return 0;
}
This is the solution. i dont know why you are writing "string reverse(string line)" out side the main() function.
i 've started learn c++ 1 week ago,i need an advice about how i can check entered word without function check_pass.How can i use if or while function for it please help.(Sorry for some mistakes )
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int enter_word;
cout<<"Hey bro what is your favourite color? ";
cin>>enter_word;
cout<<"So what is my favourite color? ";
if (enter_word="yellow"){ cout<<"Yep you are right bro!";}
system("pause");
return 0;
}
There are two major mistakes in the code you show: First is that enter_word is not a std::string object, it's an integer variable so can only contain integers. Secondly, you don't compare enter_word to "yellow" in the condition, you assign to the variable.
The first problem is solved by including <string> and declare enter_word as a string:
std::string enter_word;
The second problem can be solved by using the equality comparison operator == instead of assignment:
enter_word == "yellow"
I've just now come across an error using arrays that seems odd, I've searched the web but it would appear this is typically something people run into when they are dealing with multidimensional arrays. The error occurs when I attempt to call a function that uses an array as a parameter. Here is the code:
#include <iostream>
#include <string>
#include <iomanip>
#include "header.h"
#include <fstream>
using namespace std;
int main(){
string make, model, licensePlate, address, name, phoneNumber;
int year, messageCode;
char choice;
bool ready[10];
string phoneNumber[10], name[10], address[10], licensePlate[10], make[10], model[10];
int year[10];
initializeArray(year[]);
The error occurs within the brackets of year[], expected an expression. Thanks in advance!
I apologize for my poor code!
Having both variables of int and an array with the same name was a terrible idea, as you do not use the brackets to pass an array. I removed the brackets and the int year variable.
Here is the revised work:
bool ready[10];
string phoneNumber[10], name[10], address[10], licensePlate[10], make[10], model[10];
int year[10];
initializeArray(year);
I want to issue the echo command inside a c++ file.
All i need to do is
echo "xml::/var/some.xml" >> /var/config
In C++ file, I tried,
system("echo" + "xml::/var/some.xml" +">> /var/config");
But it throws invalid operands of types const char[6] and const char[46] to binary operator +.
Need help
I guess this is one where you may be able to get away with "can has codez?":
#include <iostream>
#include <iterator>
#include <algorithm>
int main(int argc, char **argv) {
std::copy(argv+1, argv+argc, std::ostream_iterator<char *>(std::cout, ""));
return 0;
}
You could just output the data yourself using stdio methods fopen/fputs/etc..
http://linux.die.net/man/3/fputs
Try
#include <string>
system(std::string("echo" + "xml::/var/some.xml" +">> /var/config").cstr())
you are passing raw character strings which do not support the + operator -- so try to use std::string instead.