Quantcast
Viewing latest article 12
Browse Latest Browse All 22

CPP Program display the position or index in the string

***************************************

Strstr() function in CPP

***************************************

program : To display the position or index in the string  s where the string t begins or -1 if doesn’t contain t

************

Source Code :

*************

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[20],t[20],*pos;
int pos1;
clrscr();
cout<<"\nEnter the main string:";
cin>>s;
cout<<"\nEnter the string to be searched:";
cin>>t;
pos=strstr(s,t);
if(pos)
pos1=pos-s+1;
else
pos1=-1;
cout<<"\nThe string is found at "<<pos1<<" position";
getch();
}
/*Output:

Enter the main string:searched

Enter the string to be searched:ched

The string is found at 5 position      */

1,522 total views, 3 views today


Viewing latest article 12
Browse Latest Browse All 22

Trending Articles