Slip 7 - A) Write a C++ program using class with data member char str[50] and function replace (char ch 1, char ch2) every occurrence of chl in str should be replaced with ch2 and return number of replacement it makes use default value for char ch2. (Use ch2 as Default Argument)

 Solution:

#include<conio.h>

#include<iostream.h>

#include<string.h>

class mystr

{

public:int replace(char *str,char,char);

};

int mystr::replace(char *str,char(),char c2='r')

{

while(str!='\0')

{

if(*str==c1)

{

*str==c2;

n++;

}

str++;

i++;

}

str=str-i;

return n;

}

int main()

{

mystr m;

char *str,c1,c2;

clrscr();

cout<<"Enter string";

cin>>str;

cout<<"Enter character which is to replace";

cin>>a;

cout<<"Number of replacement="<<m.replace(str,c1,c2);

cout<<"After replacement string is ="<<str;

getch();

return 0;

Post a Comment

0 Comments