0
C++ program
write program which on give correct outputs if input is String or else incorrect when it has any no-or any special character. https://code.sololearn.com/cMDdFaChr69F/?ref=app
4 ответов
+ 8
Hello, 😊
Please, if you want us to help you, then show us your attempt, what you don't understand exactly, where you are struggling, it will be much easier!👍😉
• Follow community RULES: https://www.sololearn.com/Content-Creation-Guidelines/
https://code.sololearn.com/WvG0MJq2dQ6y/
https://code.sololearn.com/W26q4WtwSP8W/?ref=app
https://www.sololearn.com/Discuss/1082512/?ref=app
+ 8
Abhay Jindal Thank you for cooperation and understanding 👍
If I understand correctly, you validate a string if it doesn't contain any "bad" character. Please confirm if I misunderstood you.
You can use built-in method `find_first_of` from std::string class, here's an example, references in comment.
#include <string>
#include <iostream>
using namespace std;
void checkstring(string str, string filter_str)
{
// REFERENCES
// string::npos
// http://en.cppreference.com/w/cpp/string/basic_string/npos
// string::find_first_of
// http://en.cppreference.com/w/cpp/string/basic_string/find_first_of
if(string::npos == str.find_first_of(filter_str))
{
// none of the characters listed in
// <filter_str> was found in <str>
cout << "Valid String";
}
else
{
// one of the characters listed in
// <filter_str> was found in <str>
cout << "Invalid String";
}
cout << endl;
}
int main()
{
string str = "SoloLearn";
string bad_char = "&|!+%@#";
checkstring(str, bad_char);
return 0;
}
Hth, cmiiw
+ 7
You're welcome Abhay Jindal.
I can't say much about efficiency, but personally I think if there is already available a built-in method for doing something, then I prefer to use it opposed to writing my own version, except of course, for educational purposes, learning the algorithm for the requirement/task. But that is just my opinion : )
+ 1
Thanks for the explanation!
Which one is more efficient though?