c 字符串处理

C语言处理字符串函数总结 目录1、字符数组的输入和输出:scanf(格式控制,地址表列(在变量前加上取地址符&))和printf(格式控制,输出表列)gets(字符数组)和puts(字符数组)注意:2、字符串连接函数:strcat(字符数组1,字符数组2)注意:3、字符串复制函数:strcpy(字符数组1,字符数串2或者字符数组名)strncpy(字符数组1,字符串2或者字符数组名,n)注意:4、字符串比较函数:s... 阅读详情
程序开头要声明
#include <string.h>
 
函数名: stpcpy
功  能: 拷贝一个字符串到另一个
用  法: char *stpcpy(char *destin, char *source);
程序例:
 
#include <stdio.h>
#include <string.h>
 
int main(void)
{
   char string[10];
   char *str1 = "abcdefghi";
 
   stpcpy(string, str1);
   printf("%s/n", string);
   return 0;
}
 
 
 
 
函数名: strcat
功  能: 字符串拼接函数
用  法: char *strcat(char *destin, char *source);
程序例:
 
#include <string.h>
#include <stdio.h>
 
int main(void)
{
   char destination[25];
   char *blank = " ", *c = "C++", *Borland = "Borland";
 
   strcpy(destination, Borland);
   strcat(destination, blank);
   strcat(destination, c);
 
   printf("%s/n", destination);
   return 0;
}
 
 
 
 
函数名: strchr
功  能: 在一个串中查找给定字符的第一个匹配之处/
用  法: char *strchr(char *str, char c);
程序例:
 
#include <string.h>
#include <stdio.h>
 
int main(void)
 {
    char string[15];
    char *ptr, c = 'r';
 
    strcpy(string, "This is a string");
    ptr = strchr(string, c);
    if (ptr)
       printf("The character %c is at position: %d/n", c, ptr-string);
    else
       printf("The character was not found/n");
    return 0;
 }
 
 
 
 
函数名: strcmp
功  能: 串比较
用  法: int strcmp(char *str1, char *str2);
看Asic码,str1>str2,返回值 > 0;两串相等,返回0
程序例:
 
#include <string.h>
#include <stdio.h>
 
int main(void)
 {
    char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
    int ptr;
 
    ptr = strcmp(buf2, buf1);
    if (ptr > 0)
       printf("buffer 2 is greater than buffer 1/n");
    else
       printf("buffer 2 is less than buffer 1/n");
 
    ptr = strcmp(buf2, buf3);
    if (ptr > 0)
       printf("buffer 2 is greater than buffer 3/n");
    else
       printf("buffer 2 is less than buffer 3/n");
 
    return 0;
 }
 
 
 
 
函数名: strncmpi
功  能: 将一个串中的一部分与另一个串比较, 不管大小写
用  法: int strncmpi(char *str1, char *str2, unsigned maxlen);
程序例:
 
#include <string.h>
#include <stdio.h>
 
int main(void)
{
   char *buf1 = "BBB", *buf2 = "bbb";
   int ptr;
 
   ptr = strcmpi(buf2, buf1);
 
   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1/n");
 
   if (ptr < 0)
      printf("buffer 2 is less than buffer 1/n");
 
   if (ptr == 0)
      printf("buffer 2 equals buffer 1/n");
 
   return 0;
}
 
 
 
 
函数名: strcpy
功  能: 串拷贝
用  法: char *strcpy(char *str1, char *str2);
程序例:
 
#include <stdio.h>
#include <string.h>
 
int main(void)
 {
    char string[10];
    char *str1 = "abcdefghi";
 
    strcpy(string, str1);
    printf("%s/n", string);
    return 0;
 }
 
 
 
 
函数名: strcspn
功  能: 在串中查找第一个给定字符集内容的段
用  法: int strcspn(char *str1, char *str2);
程序例:
 
#include <stdio.h>
#include <string.h>
#include <alloc.h>
 
int main(void)
 {
    char *string1 = "1234567890";
    char *string2 = "747DC8";
    int length;
 
    length = strcspn(string1, string2);
    printf("Character where strings intersect is at position %d/n", length);
 
    return 0;
 }
 
 
 
 
函数名: strdup
功  能: 将串拷贝到新建的位置处
用  法: char *strdup(char *str);
程序例:
 
#include <stdio.h>
#include <string.h>
#include <alloc.h>
 
int main(void)
 {
    char *dup_str, *string = "abcde";
 
    dup_str = strdup(string);
    printf("%s/n", dup_str);
    free(dup_str);
 
    return 0;
 }
 
 
 
 
函数名: stricmp
功  能: 以大小写不敏感方式比较两个串
用  法: int stricmp(char *str1, char *str2);
程序例:
 
#include <string.h>
#include <stdio.h>
 
int main(void)
{
   char *buf1 = "BBB", *buf2 = "bbb";
   int ptr;
 
   ptr = stricmp(buf2, buf1);
 
   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1/n");
 
   if (ptr < 0)
      printf("buffer 2 is less than buffer 1/n");
 
   if (ptr == 0)
      printf("buffer 2 equals buffer 1/n");
 
   return 0;
}
 
 
 
函数名: strerror
功  能: 返回指向错误信息字符串的指针
用  法: char *strerror(int errnum);
程序例:
 
#include <stdio.h>
#include <errno.h>
 
int main(void)
{
   char *buffer;
   buffer = strerror(errno);
   printf("Error: %s/n", buffer);
   return 0;
}
 
 
 
 
函数名: strcmpi
功  能: 将一个串与另一个比较, 不管大小写
用  法: int strcmpi(char *str1, char *str2);
程序例:
 
#include <string.h>
#include <stdio.h>
 
int main(void)
{
   char *buf1 = "BBB", *buf2 = "bbb";
   int ptr;
 
   ptr = strcmpi(buf2, buf1);
 
   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1/n");
 
   if (ptr < 0)
      printf("buffer 2 is less than buffer 1/n");
 
   if (ptr == 0)
      printf("buffer 2 equals buffer 1/n");
 
   return 0;
}
 
 
 
 
函数名: strncmp
功  能: 串比较
用  法: int strncmp(char *str1, char *str2, int maxlen);
程序例:
 
#include <string.h>
#include <stdio.h>
 
int  main(void)
 
{
   char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
   int ptr;
 
   ptr = strncmp(buf2,buf1,3);
   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1/n");
   else
      printf("buffer 2 is less than buffer 1/n");
 
   ptr = strncmp(buf2,buf3,3);
   if (ptr > 0)
      printf("buffer 2 is greater than buffer 3/n");
   else
      printf("buffer 2 is less than buffer 3/n");
 
   return(0);
}
 
 
 
函数名: strncmpi
功  能: 把串中的一部分与另一串中的一部分比较, 不管大小写
用  法: int strncmpi(char *str1, char *str2);
程序例:
 
#include <string.h>
#include <stdio.h>
 
int main(void)
{
   char *buf1 = "BBBccc", *buf2 = "bbbccc";
   int ptr;
 
   ptr = strncmpi(buf2,buf1,3);
 
   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1/n");
 
   if (ptr < 0)
      printf("buffer 2 is less than buffer 1/n");
 
   if (ptr == 0)
      printf("buffer 2 equals buffer 1/n");
 
   return 0;
}
 
 
 
函数名: strncpy
功  能: 串拷贝
用  法: char *strncpy(char *destin, char *source, int maxlen);
程序例:
 
#include <stdio.h>
#include <string.h>
 
int main(void)
{
   char string[10];
   char *str1 = "abcdefghi";
 
   strncpy(string, str1, 3);
   string[3] = '/0';
   printf("%s/n", string);
   return 0;
}
 
 
 
函数名: strnicmp
功  能: 不注重大小写地比较两个串
用  法: int strnicmp(char *str1, char *str2, unsigned maxlen);
程序例:
 
#include <string.h>
#include <stdio.h>
 
int main(void)
{
   char *buf1 = "BBBccc", *buf2 = "bbbccc";
   int ptr;
 
   ptr = strnicmp(buf2, buf1, 3);
 
   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1/n");
 
   if (ptr < 0)
      printf("buffer 2 is less than buffer 1/n");
 
   if (ptr == 0)
      printf("buffer 2 equals buffer 1/n");
 
   return 0;
}
 
 
 
 
函数名: strnset
功  能: 将一个串中的所有字符都设为指定字符
用  法: char *strnset(char *str, char ch, unsigned n);
程序例:
 
#include <stdio.h>
#include <string.h>
 
int main(void)
{
   char *string = "abcdefghijklmnopqrstuvwxyz";
   char letter = 'x';
 
   printf("string before strnset: %s/n", string);
   strnset(string, letter, 13);
   printf("string after  strnset: %s/n", string);
 
   return 0;
}
 
 
 
函数名: strpbrk
功  能: 在串中查找给定字符集中的字符
用  法: char *strpbrk(char *str1, char *str2);
程序例:
 
#include <stdio.h>
#include <string.h>
 
int main(void)
{
   char *string1 = "abcdefghijklmnopqrstuvwxyz";
   char *string2 = "onm";
   char *ptr;
 
   ptr = strpbrk(string1, string2);
 
   if (ptr)
      printf("strpbrk found first character: %c/n", *ptr);
   else
      printf("strpbrk didn't find character in set/n");
 
   return 0;
}
 
 
 
 
函数名: strrchr
功  能: 在串中查找指定字符的最后一个出现
用  法: char *strrchr(char *str, char c);
程序例:
 
#include <string.h>
#include <stdio.h>
 
int main(void)
{
   char string[15];
   char *ptr, c = 'r';
 
   strcpy(string, "This is a string");
   ptr = strrchr(string, c);
   if (ptr)
      printf("The character %c is at position: %d/n", c, ptr-string);
   else
      printf("The character was not found/n");
   return 0;
}
 
 
 
 
函数名: strrev
功  能: 串倒转
用  法: char *strrev(char *str);
程序例:
 
#include <string.h>
#include <stdio.h>
 
int main(void)
{
   char *forward = "string";
 
   printf("Before strrev(): %s/n", forward);
   strrev(forward);
   printf("After strrev():  %s/n", forward);
   return 0;
}
 
 
函数名: strset
功  能: 将一个串中的所有字符都设为指定字符
用  法: char *strset(char *str, char c);
程序例:
 
#include <stdio.h>
#include <string.h>
 
int main(void)
{
   char string[10] = "123456789";
   char symbol = 'c';
 
   printf("Before strset(): %s/n", string);
   strset(string, symbol);
   printf("After strset():  %s/n", string);
   return 0;
}
 
 
 
 
函数名: strspn
功  能: 在串中查找指定字符集的子集的第一次出现
用  法: int strspn(char *str1, char *str2);
程序例:
 
#include <stdio.h>
#include <string.h>
#include <alloc.h>
 
int main(void)
{
   char *string1 = "1234567890";
   char *string2 = "123DC8";
   int length;
 
   length = strspn(string1, string2);
   printf("Character where strings differ is at position %d/n", length);
   return 0;
}
 
 
 
函数名: strstr
功  能: 在串中查找指定字符串的第一次出现
用  法: char *strstr(char *str1, char *str2);
程序例:
 
#include <stdio.h>
#include <string.h>
 
int main(void)
{
   char *str1 = "Borland International", *str2 = "nation", *ptr;
 
   ptr = strstr(str1, str2);
   printf("The substring is: %s/n", ptr);
   return 0;
}
 
 
 
函数名: strtod
功  能: 将字符串转换为double型值
用  法: double strtod(char *str, char **endptr);
程序例:
 
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
   char input[80], *endptr;
   double value;
 
   printf("Enter a floating point number:");
   gets(input);
   value = strtod(input, &endptr);
   printf("The string is %s the number is %lf/n", input, value);
   return 0;
}
 
 
 
 
函数名: strtok
功  能: 查找由在第二个串中指定的分界符分隔开的单词
用  法: char *strtok(char *str1, char *str2);
程序例:
 
#include <string.h>
#include <stdio.h>
 
int main(void)
{
   char input[16] = "abc,d";
   char *p;
 
   /* strtok places a NULL terminator
   in front of the token, if found */
   p = strtok(input, ",");
   if (p)   printf("%s/n", p);
 
   /* A second call to strtok using a NULL
   as the first parameter returns a pointer
   to the character following the token  */
   p = strtok(NULL, ",");
   if (p)   printf("%s/n", p);
   return 0;
}
 
 
 
 
函数名: strtol
功  能: 将串转换为长整数
用  法: long strtol(char *str, char **endptr, int base);
程序例:
 
#include <stdlib.h>
#include <stdio.h>
 
int main(void)
{
   char *string = "87654321", *endptr;
   long lnumber;
 
   /* strtol converts string to long integer  */
   lnumber = strtol(string, &endptr, 10);
   printf("string = %s  long = %ld/n", string, lnumber);
 
   return 0;
}
 
 
函数名: strupr
功  能: 将串中的小写字母转换为大写字母
用  法: char *strupr(char *str);
程序例:
 
#include <stdio.h>
#include <string.h>
 
int main(void)
{
   char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;
 
   /* converts string to upper case characters */
   ptr = strupr(string);
   printf("%s/n", ptr);
   return 0;
}
 
 
 
 
函数名: swab
功  能: 交换字节
用  法: void swab (char *from, char *to, int nbytes);
程序例:
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
char source[15] = "rFna koBlrna d";
char target[15];
 
int main(void)
{
   swab(source, target, strlen(source));
   printf("This is target: %s/n", target);
   return 0;
}
[C语言] 指针处理字符串 在C语言中,字符串(string,简称串)是一种特殊的char型一维数组。可以把字符串中的字符作为数组中的元素访问,或利用char型指针对其访问。 字符串和字符指针 首先是字符串常量的存储,字符串常量是用一对双引号括起来的字符序列,与基本类型常量的存储相似,字符串常量在内存中的存放位置由系统自动安排。 由于字符串常量是一串字符,通常被视为一个特殊的一维字符串数组,与数组的存储类似,字符串常量中的所... 阅读详情

相关推荐

C语言的字符串处理

在C语言中,字符串是以字符数组的形式存储的一串字符。字符串以一个特殊的字符结束,称为空字符('\0'),它用来标识字符串的结束。c// 定义一个容量为100的字符数组C语言中的字符串处理是其编程语言特性的重要组成部分。虽然字符串在C语言中并不是一种原生数据类型,但通过字符数组及其标准库函数,我们能够进行丰富的字符串操作。掌握这些基本操作和标准库函数,对于开发各种应用程序都是十分重要的。本文简单介绍了字符串的定义、基本操作、标准库函数,以及一些常见的操作示例。

2401_90085933的博客 1598

在C语言中如何处理字符串,C语言中字符串的处理方式

《C语言中字符串的处理方式》由会员分享,可在线阅读,更多相关《C语言中字符串的处理方式(4页珍藏版)》请在人人文库网上搜索。1、C语言中字符串的处理方式“很难从字符串中找到感觉,但它们却是我们能指望的唯一交流纽带”-C语言程序设计 现代方法写多了 Java 代码,对 String 类 很是喜爱,可惜经典的 C 语言没有。最近在做程序过程中,发现对C语言字符串的处理很模糊,一会儿用数组,一会儿用指针...

weixin_29130369的博客 2843

C.字符串处理

C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在常量字符串 中或者 字符数组 中。 ​ 字符串常量 适用于那些对它不做修改的字符串函数。 strlen(); strcpy(); strncpy(); strcmp(); strncmp(); strchr(); strstr(); atoi(); atol(); atof(); strtok(); strerror(); toupper(); tolower();

傻响雨 961

【C语言】字符串处理指南

【C语言】字符串处理完全指南

你好 942

如何在 C 语言中处理字符串?

C 语言中的字符串处理 在 C 语言中,字符串的处理是一个重要的部分。字符串是一系列字符的集合,以空字符 `'\0'` 作为结束标志。函数用于从标准输入读取一行字符串(包括空格),直到遇到换行符为止。但由于它无法限制输入字符串的长度,可能导致缓冲区溢出,所以不推荐使用。 `puts` 函数用于输出字符串,并在末尾自动添加换行符。

2401_86074221的博客 1083

C语言字符串的输入输出处理

写在前面:这里是小王成长日志,一名在校大学生,想在学习之余将自己的学习笔记分享出来,记录自己的成长轨迹,帮助可能需要的人。欢迎关注与留言。 题目: 题目链接:https://www.dotcpp.com/oj/problem1094.html 题目 1094: 字符串的输入输出处理 时间限制: 1Sec 内存限制: 64MB 提交: 13472 解决: 4902 题目描述 字符串的输入输出处理。 输入 第一行是一个正整数N,最大为100。之后是多行字符串(行数大于N), 每一行字符串可能含有空格,字符数.

北北北 2659

C语言—— 字符串处理

1. memcpy 1.1 函数介绍 1.1.1 描述 C 库函数 void *memcpy(void *str1, const void *str2, size_t n) 从存储区 str2 复制 n 个字节到存储区 str1。 1.1.2 参数 str1 – 指向用于存储复制内容的目标数组,类型强制转换为 void* 指针。 str2 – 指向要复制的数据源,类型强制转换为 void* 指针。 n – 要被复制的字节数。 1.2用法示例 1.2.1 把数组B赋值给数组A /* * - 功能:转置

苏释州的博客 1404

Xshell 连接 kali 虚拟机教程

启动ssh服务,启动后重新查看ssh服务状态,如下图active(running)正常。至此便可以直接使用Xshell登录kali系统了。至此虚拟机已经可以上网。-查看ssh服务状态。

qq_40726937的博客 2219

C语言-字符串处理

字符串在C语言里使用非常多,因为很多数据处理都是文本,也就是字符串,特别是设备交互、web网页交互返回的几乎都是文本数据。

4352

基于STM32的可穿戴式多生理参数实时监测与预警系统

针对传统可穿戴健康设备存在的生理参数监测种类单一、数据处理依赖云端导致响应滞后、续航能力弱等问题,设计一种基于STM32的可穿戴式多生理参数实时监测与预警系统。系统以STM32L476RG低功耗微控制器为核心,集成心率血氧传感器、体温传感器、血压传感器,实现人体心率、血氧饱和度、体温、收缩压/舒张压的同步采集;通过边缘端轻量化数据处理算法完成参数异常判定,结合低功耗硬件设计与软件休眠策略,在保证监测精度的同时延长续航。测试结果表明,系统心率监测误差≤2次/分钟,血氧饱和度误差≤2%,体温误差≤±0.2℃,血

m0_74026776的博客 2041

分位数映射数据后处理-Quantile Mapping后处理 (R语言)

统计后处理器基本上是统计模型,将观察到的关注变量与从气象或水文模型的直接模型输出(DMO)导出的适当预测变量相关联。统计后处理的重要性早已在气象预报中得到认可。早期的工作包括完善的预测(perfect prognosis),模型输出统计(MOS)和模拟方法(analog method)等模型。 近年来,已经提出了许多其他后处理方法,包括秩直方图校准(rank histogram calibrati...

水滴石穿,绳锯木断 1万+

c语言处理字符串

c语言处理字符串 在物联网项目开发过程中,经常使用http get(平台会提供相应的api接口)从服务器端获取一些数据,这些数据可能时Json格式的字符串,比较好处理,但是对于部分无规则的字符串,我们如何解析它呢? 首先介绍下ubuntu下man工具的使用 安装c语言库函数man手册,sudo apt-get install manpages-dev,man 3 c语言库函数 查询库函数用法...

qq_38240926的博客 598

Unity性能优化 - 动态图集

Unity 动态图集是 Unity 引擎中用于处理游戏纹理优化的一种技术。本文介绍了什么是动态图集,动态图集的优缺点,以及提供了一套完整的代码供大家参考。

qq_33808037的博客 4840

C语言之字符串处理函数

函数功能:在字符串中查找给定字符的最后一次匹配,也就是在字符串str中查找字符c最后一次出现的位置返回值:最后一次匹配位置的指针if (ptr)elseif (ptr)elsegetch();return 0;运行结果是:注意:本例程中,如果字符串中只有一个’o’字符,那么无论调用哪种字符串中字符匹配函数都会返回相同的结果。

上善若泪 1771

【Arduino 动手做】构建一个迷你 Las Vegas RGB LED 球体

《Arduino迷你拉斯维加斯球体项目:15岁少年的创意挑战》 15岁的Ben通过本项目展示了如何利用Arduino平台制作一个经济实惠的迷你拉斯维加斯球体夜灯。该项目涉及LED选择(采用WS2812B可编程灯带)、电子元件配置(含Arduino Nano微控制器和红外遥控)以及3D建模设计(100毫米直径球体)。项目难点包括LED布局优化、供电方案设计和3D打印外壳制作。Ben详细记录了从背景研究、电路设计到最终实现的完整过程,特别强调了分辨率与成本的平衡,以及如何通过白色扩散器改善视觉效果。这个项目不仅

雕爷学编程 1024

Windows11搭建GPU版本PyTorch 开发环境教程

Windows11搭建GPU版本PyTorch环境详细过程

世界那么大,我想去看看~【随心更新,看世界去了😄】 3236
上一篇: BusyBox原理简单分析
下一篇: 硬盘主引导扇区
frog845
博客等级 码龄20年 4粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值