Program to find a sub string within a string
Object: Write a program to find a sub string within a string. If found display its starting position. C++ projects for beginners with source code
Code:
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
void main()
{
int find;
string sb2,fn1;
string s1=”Yes, we went to Gates after we left the room”;
cout<<s1;
cout<<“\nEnter substring which you find: “;
getline(cin,sb2);
find=s1.find(sb2);
fn1=s1.substr(find,’sb2′);
cout<<endl<<“Substring is: “<<fn1;
cout<<endl<<“Substring Position is: “<<find;
getch();
}
#include<iostream> #include<conio.h> #include<string> using namespace std; void main() { int find; string sb2,fn1; string s1="Yes, we went to Gates after we left the room"; cout<<s1; cout<<"\nEnter substring which you find: "; getline(cin,sb2); find=s1.find(sb2); fn1=s1.substr(find,'sb2'); cout<<endl<<"Substring is: "<<fn1; cout<<endl<<"Substring Position is: "<<find; getch(); }
Pingback: C++ projects for beginners with source code - Codeboks