首页 > 编程语言 > 详细

C语言字符串处理

时间:2021-05-12 09:55:09      阅读:33      评论:0      收藏:0      [点我收藏+]

C语言的字符串操作

1.strtok 实现字符串切割: 将字符串根据分隔符进行切割分片.

#include <stdio.h>
 
int main(int argc, char* argv[])
{
    char str[] = "hello,lyshark,welcome";
    char *ptr;
 
    ptr = strtok(str, ",");
    while (ptr != NULL)
    {
        printf("切割元素: %s\n", ptr);
        ptr = strtok(NULL, ",");
    }
    system("pause");
    return 0;
}

2.strlen 获取字符串长度

#include <stdio.h>
 
int main(int argc, char* argv[])
{
    char Array[] = "\0hello\nlyshark";
    char Str[] = { h, e, l, l, o };
 
    int array_len = strlen(Array);
    printf("字符串的有效长度:%d\n", array_len);
    int str_len = strlen(Str);
    printf("字符串数组有效长度: %d\n", str_len);
    
    int index = 0;
    while (Str[index] != \0)
    {
        index++;
        printf("Str数组元素: %c --> 计数: %d \n", Str[index], index);
    }
 
    system("pause");
    return 0;
}

3.strcpy 字符串拷贝:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main(int argc, char* argv[])
{
    char Array[] = "hello lyshark";
    char tmp[100];
 
    // 学习strcpy函数的使用方式
    if (strcpy(tmp, Array) == NULL)
        printf("从Array拷贝到tmp失败\n");
    else
        printf("拷贝后打印: %s\n", tmp);
 
    // 清空tmp数组的两种方式
    for (unsigned int x = 0; x < strlen(tmp); x++)
        tmp[x] =  ;
 
    memset(tmp, 0, sizeof(tmp));
    
    // 学习strncpy函数的使用方式
    if (strncpy(tmp, Array, 3) == NULL)
        printf("从Array拷贝3个字符到tmp失败\n");
    else
        printf("拷贝后打印: %s\n", tmp);
 
    system("pause");
    return 0;
}

4.strcat字符串连接: 将由src指向的空终止字节串的副本追加到由dest指向的以空字节终止的字节串的末尾

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main(int argc, char* argv[])
{
    char str1[50] = "hello ";
    char str2[50] = "lyshark!";
 
    char * str = strcat(str1, str2);
    printf("字符串连接: %s \n", str);
 
    str = strcat(str1, " world");
    printf("字符串连接: %s \n", str);
 
    str = strncat(str1, str2, 3);
    printf("字符串连接: %s \n", str);
 
    system("pause");
    return 0;
}

5.strcmp 字符串对比:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int Str_Cmp(const char * lhs, const char * rhs)
{
    int ret = strcmp(lhs, rhs);
    if (ret == 0)
        return 1;
    else
        return 0;
}
 
int main(int argc, char* argv[])
{
    char *str1 = "hello lyshark";
    char *str2 = "hello lyshark";
 
    int ret = Str_Cmp(str1, str2);
    printf("字符串是否相等: %d \n", ret);
 
    if (!strncmp(str1, str2, 3))
        printf("两个字符串,前三位相等");
 
    system("pause");
    return 0;
}

6.strshr 字符串截取:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main(int argc, char* argv[])
{
    const char str[] = "hello ! lyshark";
    char *ret;
 
    ret = strchr(str, !);
    printf("%s \n", ret);
 
    system("pause");
    return 0;
}

7.字符串逆序排列:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
void Swap_Str(char *Array)
{
    int len = strlen(Array);
    char *p1 = Array;
    char *p2 = &Array[len - 1];
    while (p1 < p2)
    {
        char tmp = *p1;
        *p1 = *p2;
        *p2 = tmp;
        p1++, p2--;
    }
}
 
int main(int argc, char* argv[])
{
    char str[20] = "hello lyshark";
    Swap_Str(str);
    
    for (int x = 0; x < strlen(str); x++)
        printf("%c", str[x]);
 
    system("pause");
    return 0;
}

8.实现字符串拷贝:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
// 使用数组实现字符串拷贝
void CopyString(char *dest,const char *source)
{
    int len = strlen(source);
    for (int x = 0; x < len; x++)
    {
        dest[x] = source[x];
    }
    dest[len] = \0;
}
 
// 使用指针的方式实现拷贝
void CopyStringPtr(char *dest, const char *source)
{
    while (*source != \0)
    {
        *dest = *source;
        ++dest, ++source;
    }
    *dest = \0;
}
// 简易版字符串拷贝
void CopyStringPtrBase(char *dest, const char *source)
{
    while (*dest++ = *source++);
}
 
int main(int argc, char* argv[])
{
    char * str = "hello lyshark";
    char buf[1024] = { 0 };
    CopyStringPtrBase(buf, str);
    printf("%s \n", buf);
 
    system("pause");
    return 0;
}

9.格式化字符串:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main(int argc, char* argv[])
{
    // 格式化填充输出
    char buf[30] = { 0 };
    sprintf(buf, "hello %s %s", "lyshark","you are good");
    printf("格式化后: %s \n", buf);
 
    // 拼接字符串
    char *s1 = "hello";
    char *s2 = "lyshark";
    memset(buf, 0, 30);
    sprintf(buf, "%s --> %s", s1, s2);
    printf("格式化后: %s \n", buf);
 
    // 数字装换位字符串
    int number = 100;
    memset(buf, 0, 30);
    sprintf(buf, "%d", number);
    printf("格式化后: %s \n", buf);
 
    system("pause");
    return 0;
}

10.动态存储字符串:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main(int argc, char* argv[])
{
    // 分配空间
    char **p = malloc(sizeof(char *)* 5);
    for (int x = 0; x < 5;++x)
    {
        p[x] = malloc(64); 
        memset(p[x], 0, 64);
        sprintf(p[x], "Name %d", x + 1);
    }
 
    // 打印字符串
    for (int x = 0; x < 5; x++)
        printf("%s \n", p[x]);
 
    // 释放空间
    for (int x = 0; x < 5; x++)
    {
        if (p[x] != NULL)
            free(p[x]);
    }
 
    system("pause");
    return 0;
}

11.字符串拼接:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
char * StringSplicing(char *String1, char  *String2)
{
    char Buffer[1024];
 
    int index = 0;
    int len = strlen(String1);
    while (String1[index] != \0)
    {
        Buffer[index] = String1[index];
        index++;
    }
    while (String2[index - len] != \0)
    {
        Buffer[index] = String2[index - len];
        index++;
    }
    Buffer[index] = \0;
 
    char *ret = (char*)calloc(1024, sizeof(char*));
    if (ret)
        strcpy(ret, Buffer);
    return ret;
}
 
int main(int argc, char* argv[])
{
    char *str1 = "hello ";
    char *str2 = "lyshark ! \n";
 
    char * new_str = StringSplicing(str1, str2);
    printf("拼接好的字符串是: %s", new_str);
 
    system("pause");
    return 0;
}

12.实现strchr:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
char * MyStrchr(const char *String, char ch)
{
    char *ptr = String;
    while (*ptr != \0)
    {
        if (*ptr == ch)
            return ptr;
        ptr++;
    }
    return NULL;
}
 
int main(int argc, char* argv[])
{
    char Str[] = "hello lyshark";
    char ch = s;
 
    char *ptr = MyStrchr(Str, ch);
    printf("输出结果: %s \n", ptr);
 
    system("pause");
    return 0;
}

13.自己实现寻找字符串子串:

#include <stdio.h>
#include <stdlib.h>
 
// 查找子串第一次出现的位置
char *MyStrStr(const char* str, const char* substr)
{
    const char *mystr = str;
    const char *mysub = substr;
 
    while (*mystr != \0)
    {
        if (*mystr != *mysub)
        {
            ++mystr;
            continue;
        }
 
        char *tmp_mystr = mystr;
        char *tmp_mysub = mysub;
 
        while (tmp_mysub != \0)
        {
            if (*tmp_mystr != *tmp_mysub)
            {
                ++mystr;
                break;
            }
             ++tmp_mysub;
        }
 
 
        if (*tmp_mysub == \0)
        {
            return mystr;
        }
    }
    return NULL;
}
 
int main(int argc, char* argv[])
{
 
    char *str = "abcdefg";
    char *sub = "fg";
 
    char * aaa = MyStrStr(str, sub);
 
    printf("%s", aaa);
 
    system("pause");
    return 0;
}

14.删除字符串中连续字符

#include <stdio.h>
char del(char s[],int pos,int len)   //自定义删除函数,这里采用覆盖方法
 {
   int i;
   for (i=pos+len-1; s[i]!=\0; i++,pos++)
     s[pos-1]=s[i];   //用删除部分后的字符依次从删除部分开始覆盖
   s[pos-1]=\0;
   return s;
 }
int main(int argc, char *argv[])
{
  char str[50];
  int position,length;
  printf ("please input string:\n");
  gets(str);   //使用gets函数获得字符串
  printf ("please input delete position:");
  scanf("%d",&position);
  printf ("please input delete length:");
  scanf("%d",&length);
  del(str,position,length);
  printf ("the final string:%s\n",str);
  return 0;
}

 

C语言字符串处理

原文:https://www.cnblogs.com/ruiyao18369/p/14758241.html

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