Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write the following fucntions and test then in the main without using <CSTRING>

ID: 3685664 • Letter: W

Question

write the following fucntions and test then in the main without using <CSTRING> library or pointer. using only namespace std, and include <iosteam>

find_first_of, and find_first_not_of. Instead of finding the first occurrence of an exact string (as find does),find_first_of finds the first occurrence of any of the characters included in a specified string.

findfirstof( source cstring, character set, starting pos=0); //first occurrence of any of the characters in character set in source

find_first_not_of finds the first occurrence of a character that is not any of the characters included in the specified string.

findfirstnotof(source cstring, character set, starting pos=0); //first occurrence of any characters in source not in he characters in character set.

Explanation / Answer

code:

#include <iostream>
using namespace std;

int find_first_of(string src_string, string char_set,int start_pos)
{
int i=0;
int occ_index=-1; //it will return -1 if such character occurence is not found
while(i<src_string.size())
{
int j=0;
while(j<char_set.size())
{
if(src_string[i]==char_set[j]) // check in char set
{
occ_index =i;
break;
}
j++;
}
if(occ_index!=-1)
break;
i++;
}
return occ_index;
}

int find_first_not_of(string src_string, string char_set,int start_pos)
{
int i=0;
int occ_index=-1;//it will return -1 if such character occurence is not found
while(i<src_string.size())
{
int j=0;
int flag=0;
while(j<char_set.size())
{
if(src_string[i]==char_set[j]) // check in char set
{
flag=1;
break;
}
j++;
}
if(flag==0)
{
occ_index =i;
}
if(occ_index!=-1)
break;
i++;
}
return occ_index;
}


int main(void)
{
string src,cset; //src for source string and cset for character set
int index1,index2;
cout<<"enter source string"<<endl;
cin>>src;
cout<<"enter character dataset"<<endl;
cin>>cset;
index1=find_first_of(src,cset,0); //returns index
index2=find_first_not_of(src,cset,0);//returns index
cout<<"find_first_of()....."<<endl;
cout<<"index is: "<<index1 << " char is: "<<src[index1]<<endl;
cout<<"find_first_not_of()....."<<endl;
cout<<"index is: "<<index2 << " char is: "<<src[index2]<<endl;   
}

Sample Input and Output:

enter source string
ankit
enter character dataset
tureoa
find_first_of().....
index is: 0 char is: a
find_first_not_of().....
index is: 1 char is: n