Implementation of string functions

Implementation of string functions

One: strlen()—-calculate the string size

Write strlen first, because it will be used by several functions later.

code show as below:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int my_strlen(char* arr)
{<!-- -->
int sum = 0, i = 0;
while (*arr != '\0')
{<!-- -->
sum + + ;
arr + + ;
}
return sum;
}
int main()
{<!-- -->
char arr[200];
gets(arr);
int sum = my_strlen(arr);
printf("%d", sum);
return 0;
}

? *arr moves one bit at a time, sum + +, until the pointer points to ‘\0’, and returns sum.

The following is the output:

Two: strcpy()—-Copy string

code show as below:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

int my_strlen(char* arr)
{<!-- -->
int sum = 0, i = 0;
while (*arr != '\0')
{<!-- -->
sum + + ;
arr + + ;
}
return sum;
}

void my_strcpy(char *a, char *b)
{<!-- -->
while (*b!='\0')
{<!-- -->
*a = *b;
a + + ;
b + + ;
}
*a = '\0';
}
 int main()
{<!-- -->
char s1[200];
char s2[200];
gets(s2);
my_strcpy(s1, s2);
int len = my_strlen(s2);
for(int i=0;i<len;i + + )
printf("%c", s1[i]);
return 0;
}

The two pointers point to s and t respectively, *b is assigned to *a, b + +, a + +, and finally the extra characters of a are changed to ‘\0’.

Here is the output:

Three: stringsert()—- Insert string t before the pos character of string s

code show as below:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

int my_strlen(char arr[])
{<!-- -->
int sum = 0, i = 0;
while (arr[i] != '\0')
{<!-- -->
sum + + ;
i + + ;
}
return sum;
}

void my_strcpy(char *a, char *b)
{<!-- -->
while (*b!='\0')
{<!-- -->
*a = *b;
a + + ;
b + + ;
}
*a = '\0';
}
void stringsert(char *s, int pos, char *t)
{<!-- -->
char* ts = s;
int len1 = my_strlen(s);
int len2 = my_strlen(t);
char ps[200] = {<!-- --> 0 };
int i,j,k;
for (i = 0; i < pos-1; i + + )
{<!-- -->
ps[i] = *ts;
ts++;
}
for (j = 0; j < len2; j + + )
{<!-- -->
ps[i + j] = *t;
t + + ;
}
for (k = 0; k < len1 + 1- pos; k + + )
{<!-- -->
ps[i + j + k] = *ts;
ts++;
}
my_strcpy(s, ps);
}
int main()
{<!-- -->
char s[2000] = {<!-- --> 0 };
char t[200] = {<!-- --> 0 };
int m;
gets(s);
scanf("%d", & amp;m);
getchar();//recycling\

gets(t);
strinsert(s, m, t);
puts(s);
return 0;
}

My idea here is to first create an array and input it into three parts. The first part is the string before pos, the second part is the inserted string, and the third part is the remaining string, which is input in three parts.

Here is the output:

Four: strdelete()—-Delete the string s starting from pos and the length of len

code show as below:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

void my_strcpy(char *a, char *b)
{<!-- -->
while (*b!='\0')
{<!-- -->
*a = *b;
a + + ;
b + + ;
}
*a = '\0';
}

int my_strlen(char* arr)
{<!-- -->
int sum = 0, i = 0;
while (*arr != '\0')
{<!-- -->
sum + + ;
arr + + ;
}
return sum;
}

void strdelete(char* s, int pos, int len)
{<!-- -->
s[pos - 1] = ' + ';
int slen = my_strlen(s);
char ps[200] = {<!-- --> 0 };
int sum = 0;
while (s[sum] != ' + ')
{<!-- -->
ps[sum] = s[sum];
sum + + ;
}
for (int i = pos -1 + len; i < slen; i + + )
{<!-- -->
ps[sum] = s[i];
sum + + ;
}
my_strcpy(s, ps);
}

int main()
{<!-- -->
char s[200] = {<!-- --> 0 };
int pos = 0, len = 0;
gets(s);
scanf("%d %d", & amp;pos, & amp;len);
strdelete(s, pos, len);
puts(s);
}

? My idea is to first determine where to delete, set it to ‘ + ‘, then create an array, store the characters before + in it, then store the remaining characters after pos -1 + len, and finally store the characters in the array Copy the string back.

Here is the output:

Five: strcompare()—-s, if t is equal, return 0. Otherwise, return the difference between the ACSII code of the first unequal character

code show as below:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

int strcompare(char* s, char* t)
{<!-- -->
while (*s == *t)
{<!-- -->
if (*s == '\0' & amp; & amp; *t == '\0')//Determine whether string s and string t are equal
return 0;
s++;
t + + ;
}
return *s - *t;
}
int main()
{<!-- -->
char s[200] = {<!-- --> 0 };
char t[200] = {<!-- --> 0 };
gets(s);
gets(t);
int ans = strcompare(s, t);
printf("%d", ans);
return 0;
}

?Judge bit by bit.

Here is the output:

6: strcat()—-Connect string t to string s

code show as below:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

int my_strlen(char* arr)
{<!-- -->
int sum = 0, i = 0;
while (*arr != '\0')
{<!-- -->
sum + + ;
arr + + ;
}
return sum;
}

void my_strcat(char* t, const char* s)
{<!-- -->
int lent = my_strlen(t);
int lens = my_strlen(s);
t = t + lent;
for (int i = 0; i < lens; i + + )
{<!-- -->
*t = *s;
t + + ;
s++;
}
}

int main()
{<!-- -->
char t[200] = {<!-- --> 0 };
char s[200] = {<!-- --> 0 };
gets(t);
gets(s);
my_strcat(t, s);
puts(t);
return 0;
}

? Find lent and lens respectively, the two pointers point to t and s, move the pointer pointing to t to the end (t + lent), and then copy.

Here is the output:

Seven: substring()—-Intercept a string of len characters starting from the pos-th character of string s, and assign it to string t

code show as below:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

void substring(char* t, const char* s, int pos, int len)
{<!-- -->
s = s + pos - 1;
for (int i = 0; i < len; i + + )
{<!-- -->
*t = *s;
s++;
t + + ;
}
}

int main()
{<!-- -->
char s[200] = {<!-- --> 0 };
char t[200] = {<!-- --> 0 };
int pos, len;
gets(s);
scanf("%d %d", & amp;pos, & amp;len);
substring(t, s, pos, len);
puts(t);
return 0;
}

? First point to the first address of the target string and assign the value.

Here is the output:

8: strindex()—-If there is a substring equal to t after the pos position, return the position, otherwise return 0

code show as below:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

int my_strlen(char* arr)
{<!-- -->
int sum = 0, i = 0;
while (*arr != '\0')
{<!-- -->
sum + + ;
arr + + ;
}
return sum;
}

char* strindex(char* s, int pos, char* t)
{<!-- -->
s = s + pos;
int i = 0;
int wz = pos;
int sum = 0;
int len = my_strlen(t);
while (*s != '\0')
{<!-- -->
sum = 0;
char* ps;
ps = s;
char* pt;
pt = t;
for (i = 0; i < len; i + + )
{<!-- -->
if (*ps == *pt)
{<!-- -->
ps++;
pt++;
sum + + ;
}
}
if (sum == len)
return wz;
s++;
wz + + ;
}
return 0;
}

int main()
{<!-- -->
char s[200] = {<!-- --> 0 };
char t[200] = {<!-- --> 0 };
int pos;
gets(s);
gets(t);
scanf("%d", & amp;pos);
int sum = strindex(s, pos, t);
printf("%d", sum);
return 0;
}

Here is the output:

9: strreplace()—-use string v to replace the bits in string s that are different from string t

code show as below:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

void strreplace(char* s, const char* t, const char* v)
{<!-- -->
while(*s!='\0')
{<!-- -->
if (*s != *t)
*s = *v;
s++;
t + + ;
v + + ;
}
}

int main()
{<!-- -->
char s[200] = {<!-- --> 0 };
char t[200] = {<!-- --> 0 };
char v[200] = {<!-- --> 0 };
gets(s);
gets(t);
gets(v);
strreplace(s, t, v);
puts(s);
return 0;
}

Here is the output:

Ten: strempty()—-Judge whether it is an empty string. If it is empty, return TRUE, otherwise return FALSE

code show as below;

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

int strempty(char* s)
{<!-- -->
if (*s == '\0')
return 0;
else
return 1;
}

int main()
{<!-- -->
char s[200];
gets(s);
if (strempty(s) == 1)
printf("not empty");
else
printf("empty");

return 0;
}

Here is the output:

Eleven: strclear()—-clear string s

code show as below:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

void strclear(char* s)
{<!-- -->
*s = '\0';
}

int main()
{<!-- -->
char s[200];
gets(s);
strclear(s);
puts(s);
return 0;
}

? Change the first bit to ‘\0’.

Here is the output:

Twelve: strdestroy()—-Destroy s

code show as below:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

void strdestroy(char* s)
{<!-- -->
free(s);
}

int main()
{<!-- -->
char s[200];
gets(s);
strdestroy(s);
puts(s);
return 0;
}

Here is the output:

It’s the end! ! !