This question already has an answer here:
\s not working in C++ regex
(1 answer)
Closed 3 years ago.
Please help............................................
My regex won't work on spaces:
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<fstream>
#include<list>
#include<numeric>
#include<map>
#include<iterator>
#include<regex>
using namespace std;
int main()
{
regex date3{R"([A-Z][a-z]{2,}\s\d{1,})"};
string s;
cin>>s;
smatch matches;
if(regex_match(s,matches,date3)){
cout<<matches[0];
}
return 0;
}
I also tried
regex date3{R"([A-Z][a-z]{2,} \d{1,})"};
For example, I input May 3 and it outputs nothing.
cin>>s splits the input on white space and discards those white spaces.
Instead you may want to use for instance getline(cin, s).
Related
In the following code, I'm getting only string a as output on my mingw compiler but I get both strings as output on an online compiler. Why am I not getting appropriate output on my system?
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string a="Welcome";
string b="HelloWorld";
for(int i=0;i<=b.length();i++){
a.push_back(b[i]);
}
cout<<a<<endl<<b;
return 0;
}
Output-
WelcomeHelloWorld
one thing you can do is, you can use cout.flush(). If it does not work you can do a+b or you can use substr()
#include<iostream>
#include<cstring>
using namespace std;
int main(){
cout.flush();
string a="Welcome";
string b="HelloWorld";
cout<<a+b<<endl<<b;
// cout<<a+b.substr(0,9);
return 0;
}
I think cout.flush() sometimes help when getting unwanted output. So, once you can try this also before printing the result.
This question already has answers here:
Trouble with C++ Regex POSIX character class
(3 answers)
Closed 4 years ago.
from what i researched, the expression "[:alpha:]" will be matched for any alphabetic character, but the expression only match for lowercase character and not uppercase character. I not sure what's wrong with it.
std::regex e ("[:alpha:]");
if(std::regex_match("A",e))
std::cout<<"hi";
else
std::cout<<"no";
Change this:
std::regex e ("[:alpha:]");
to:
std::regex e ("[[:alpha:]]");
As Adrian stated: Please note that the brackets in the class names are additional to those opening and closing the class definition. For example: [[:alpha:]] is a character class that matches any alphabetic character. Read more in the ref.
You have to use [[:alpha:]]
see online example
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main() {
std::regex e ("[[:alpha:]]");
if(std::regex_match("A",e))
std::cout<<"hi";
else
std::cout<<"no";
return 0;
}
So thing is I can copy paste unicode characters like chess pieces directly to terminal( I'm using debian jessie linux) but whenever I write c++ code to do that, I get these � instead
here is my code
enter code here
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
setlocale(LC_ALL,"");
wchar_t piece='♗';
wcout<<piece;
}
I tried to use the hex or decimal code of the characters but it does not work
I also use vim to edit and it does show the characters while I'm typing.
There's no specification of what encoding should be used for wchar_t. I need to use mbstowcs function to convert that character. Like this, for example:
#include <iostream>
#include <clocale>
#include <cstdlib>
using namespace std;
int main(void) {
setlocale(LC_ALL, "");
wchar_t piece;
mbstowcs(&piece, "♗", 1);
wcout << piece << endl;
return 0;
}
assuming your source file encoding matches the encoding of your locale.
Oddly enough what worked was going at it normally and putting the special character into a string it's so ridiculously simple I didn't even think to use it.
#include<iostream>
using namespace std;
int main()
{
string piece="♗";
cout<<piece;
}
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
No output for cout
i writed this code in c++ for my uni , but i have an error in return 0 , the code don't work . i am using xcode to develop
#include <iostream>
#include <string>
using std::string;
int main( void )
{
string portF("PORTOFINO IM SOMMER 2012");
std::cout<<portF<<
portF.erase(0,5);
portF.insert(3,"IT");
portF.erase(7,3);
portF.insert(13,"SEMESTER");
portF.append("!");
std::cout<<portF<<
return 0;
}
std::cout<<portF<<
should be
std::cout<<portF;
Note you've made the same error twice. A semicolon is what ends a statement. When you put an insertion operator instead of it, compiler expects another expression (and that's what it is telling you).
use:
std::cout<<portF;
instead of,
std::cout<<portF<<
1. You haven't added a semicolon after the statement.
2. you are using one extra << operator
#include <iostream>
#include <string>
using std::string;
int main( void )
{
string portF("PORTOFINO IM SOMMER 2012");
// You have to end this statement with semi colon
std::cout<<portF;
portF.erase(0,5);
portF.insert(3,"IT");
portF.erase(7,3);
portF.insert(13,"SEMESTER");
portF.append("!");
// Similarily here
std::cout<<portF;
return 0;
}