topic description
string insertion StrInsert operation condition: string sscore t exists, 1 I strength (s) + 1. Operation result: the string t is inserted into the first character position of the string s, and the string value of s changes.
sources of topics and their own ideas
Source: data structure on the computer
idea: directly use the pointer to move and assign characters in the character array
related codes
void StrInsert(char * s, int i, const char * t)
{
if (i<1 || i>StrLength(s) + 1)
{
puts("StrInsert");
return;
}
char * p1;
p1 = s;
int j;
p1 = p1 + i - 1;
for (j = 0; j < StrLength(s) - i + 1; jPP)
*(p1 + StrLength(s) + StrLength(t) - i - j) = *(p1 + StrLength(s) - i - j);//
s[StrLength(s) + StrLength(t)] = "\0";
for (j = 0; j < StrLength(t); jPP)
*(p1 + j) = t[j];//
}
what result do you expect? What is the error message actually seen?
when the string t has only one character, it is assumed that the string is "d"
. When the string s to be inserted is assigned in the form of char a [20] = "abc";, call this function StrInsert (aline 2, "d") to make a = "adbc"
when the string s to be inserted is assigned as char a [20]; scanf ("% s", a);. Calling this function directly results in a memory read error, and the printed string is similar to "hot."
I hope someone can answer this question. Thank you very much!