Strrep () replaces a substring in a string with the contents of another substring. This method does not change the length of the string, and if the substituted substring is longer than the atomic string, the contents of the original string are overwritten.
Strins () Inserts a string into the original string, and the total length of the string is the sum of two strings when the insertion completes.
Strdel () is the starting point of POS at a particular location in S1, and the substring is deleted according to Len length.
#include <stdio.h>
#include <string.h>
Substitution of Char *strrep (char *s1,char *s2,int POS)//String
{
int i,j;
Pos--;//calculates the starting position of the substitution, minus 1 makes it the index value (counting from 0)
i=0;
for (j=pos;s1[j]!= '/0 '; j + +) starts at the beginning of the substitution
{
if (s2[i]!= '/0 ')
{
s1[j]=s2[i];//to replace
i++;
}
Else
{
Break
}
}
return s1;
}
Insert of Char *strins (char *s1,char *s2,int POS)//String
{
int s1_length,s2_length;
int i,j;
pos--;
S1_length=strlen (S1);
S2_length=strlen (S2);
for (i=s1_length;i>=pos;i--)//moves all characters after the insertion position backward to empty the character length required for S2
{
S1[i+s2_length]=s1[i];
}
for (j=pos;s2[j-pos]!= '/0 '; j + +) fills the contents of the string S2 into the empty string S1
{
S1[j]=s2[j-pos];
}
return s1;
}
Char *strdel (char *s,int pos,int len)/String deletion
{
int i;
pos--;
for (i=pos+len;s[i]!= '/0 '; i++)//Starting from POS Delete string
{
S[i-len]=s[i];
}
S[i-len]= '/0 ';
return s;
}
void Main ()
{
Char string1[100];
Char string2[50];
Char string3[100];
Char string4[50];
Char string[50];
Char temp[5];
int position1;
int position2;
int Position3;
int length;
printf ("/nplease input original string:");
Gets (string1);
printf ("/nplease input substitute string:");
Gets (string2);
printf ("/nplease input substitute position:");
scanf ("%d", &position1); The starting position of the input substitution (count from 1)
Gets (temp); the gets () here is used to receive the "carriage return" at the end of the previous input line
Strrep (String1,string2,position1);
printf ("/nthe final string:%s/n", string1);
printf ("/nplease input original string:");
Gets (STRING3);
printf ("/ninsertion string:");
Gets (STRING4);
printf ("/ninsertion position:");
scanf ("%d", &position2); Enter the starting position of the insertion string (counting from 1)
Gets (temp); the gets () here is used to receive the "carriage return" at the end of the previous input line
Strins (String3,string4,position2);
printf ("/nthe final string:%s/n", string3);
printf ("/nplease input original string:");
Gets (string);
printf ("/nplease input Delete position:");
scanf ("%d", &position3); Enter the starting position to delete (count from 1)
printf ("/nplease input Delete length:");
scanf ("%d", &length); Enter the length of the string you want to delete
Strdel (string,position3,length);
printf ("/nthe final string:%s/n", string);
}
Summary: The use of Gets () function: Gets () is used to enter a line of string, with spaces in the middle, ending with a carriage return. In the above function, a gets function is added after scanf, the reason is that when a character is received from the keyboard using the SCANF function, it reads only the character itself, and the carriage return after the character is left in the input buffer, which is read by the gets function as the first character of the type string. The gets function stops reading when it encounters a carriage return, so String3 is an empty string. So before you define a character array, temp, and then add a gets (temp) statement after the SCANF function to receive the "carriage return" at the end of the previous input line to ensure that the next line of string input is properly received.