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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>
using namespace std;
const int MaxChars = 51; //Declare global constants.
int findSubStr(char [], char []); // Function prototypes.
void deleteSubstr(char [], char[]);
int findSubStr(char str[], char subStr[]) // FindSubStr function.
{
int pos = -1, i, r, len1, len2;
len1 = strlen(str);
len2 = strlen(subStr);
for (i=0; i<len1-len2; i++)
{
r= strncmp (str+i, subStr, len2);
if (r==0)
{
pos=i;
break;
}
}
return(pos);
}
void deleteSubStr(char str[], char subStr[]) // deleteSubStr function.
{
char temp[MaxChars];
int pos, len1;
len1 = strlen(str);
pos = findSubStr(str, subStr);
strncpy(temp, str, pos-1);
strcpy(temp+pos+3, str);
strcpy(str, temp);
}
int main() // Main function.
{
char str[MaxChars]= "She sells sea shells by the sea shore";
char subStr[MaxChars]="sea";
cout << deleteSubStr(str, subStr) << endl;
return 0;
}
|