Program to find a substring within a string. If found display its starting position

Program to find a sub string within a string

Program to find a substring within a string. If found display its starting position

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();
}

Output:

Program to find a substring within a string. If found display its starting position

1 thought on “Program to find a substring within a string. If found display its starting position”

  1. Pingback: C++ projects for beginners with source code - Codeboks

Leave a Comment

Your email address will not be published. Required fields are marked *