Prateek is a newbie in C++ programming. He wants to write a program in C++, where he needs to read a string and check whether the string is present.
#include
#include
int main()
{
char ptr[100];
char ch;
//Your code goes here
std::cout << ch << " is present in " << ptr <<"\n";
//Your code goes here
std::cout << ch << " is not present in " << ptr << "\n";
}
Answers
Answer:How do you check if a word is in a string C++?
You can try using the find function: string str ("There are two needles in this haystack."); string str2 ("needle"); if (str. find(str2) != string::npos) { //.. found. }
How do I get the position of a character in a string C++?
returns the position of first instance of character c into string s or std::string::npos in case the character is not present at all. You can also pass the starting index for the search; i.e. will return the first index of character c but starting the search from position x0.
How do you check if a substring is present in a string?
You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accept a String and return starting position of the string if it exists, otherwise it will return -1.
plz mark the brainliest
Answer:
#include<string>
#include <iostream>
using namespace std;
int main()
{
string ptr;
string ch;
getline(cin,ptr);
getline(cin,ch);
int found =-1;
found=ptr.find(ch);
if(found!=-1)
std::cout << ch << " is present in " << ptr <<"\n";
else
std::cout << ch << " is not present in " << ptr << "\n";
}
Explanation: