首页 > 其他 > 详细

字符串和字符串函数

时间:2020-11-17 09:48:43      阅读:26      评论:0      收藏:0      [点我收藏+]
1.模拟实现strcat

#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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!