#include<stdio.h>
#pragma warning(disable:4996)
void strcat_fun(char* s1, char* s2)
{
int count = 0;
while (*s1 != ‘\0‘)
{
count++;
s1++;
}
while (*s2 != ‘\0‘)
{
*(s1++) = *(s2++);
}
}
int main()
{
char arr[10] = { "how" };
strcat_fun(arr, "are");
printf("%s\n",arr);
return 0;
}
2.模拟实现strcmp
#include<stdio.h>
int strcmp(char* s1, char* s2)
{
for (; *s1 != ‘\0‘ || *s2 != ‘\0‘; s1++, s2++)
{
if (*s1 > *s2)
{
return 1;
}
else if (*s1 < *s2)
{
return -1;
}
else
{
continue;
}
}
return 0;
}
int main()
{
char* str1 = "hello";
char* str2 = "hello";
int k = strcmp(str1, str2);
printf("%d\n", k);
return 0;
}
3.模拟实现strcpy
#include<stdio.h>
#pragma warning(disable:4996)
void strcpy_func(char* s1, char* s2)
{
while (*s2 != ‘\0‘)
{
*(s1++) = *(s2++);
}
}
int main()
{
char arr1[100] = "hello world";
char arr2[10] = "boy";
strcpy_func(arr1, arr2);
printf("%s\n", arr1);
}
4.模拟实现strlen
#include<stdio.h>
#pragma warning(disable:4996)
void strcpy_func(char* s1, char* s2)
{
while (*s2 != ‘\0‘)
{
*(s1++) = *(s2++);
}
}
int main()
{
char arr1[100] = "hello world";
char arr2[10] = "boy";
strcpy_func(arr1, arr2);
printf("%s\n", arr1);
}
5.strtok 实现字符串的反转
#include<stdio.h>
#include<string.h>
#pragma warning(disable:4996)
void reverse(char* str)
{
char* arr[10];
char* ch;
ch = strtok(str," ");
int i = 0;
while (ch != NULL)
{
arr[i++] = ch;
ch = strtok(NULL, " ");
}
for (int j = i - 1; j >= 0; j--)
{
printf("%s ", arr[j]);
}
}
int main()
{
char arr[100] = "we are students.";
reverse(arr);
return 0;
}
6.strncpy
#include<stdio.h>
#include<string.h>
#pragma warning(disable:4996)
int main()
{
char str1[100] = "hello world";
char str2[100] = "free of charge";
strncpy(str1, str2, 7);
printf("%s\n", str1);
}
7.strncat
#include<stdio.h>
#include<string.h>
#pragma warning(disable:4996)
int main()
{
char str1[100] = "hello world";
char str2[100] = "free of charge";
strncat(str1, str2, 7);
printf("%s\n", str1);
return 0;
}
8.strcmp
#include<stdio.h>
#include<string.h>
#pragma warning(disable:4996)
int main()
{
char str1[100] = "helo orld";
char str2[100] = "hello o";
int k = strncmp(str1, str2, 7);
printf("%d\n",k);
return 0;
}
9.strstr
#include<stdio.h>
#include<string.h>
#pragma warning(disable:4996)
int main()
{
char str[100] = " bit hello bit hello world";
char* s;
s = strstr(str, "hello");
printf("%s\n", s);
}
原文:https://blog.51cto.com/14946385/2551440