c语言字符串函数有哪些(c语言字符串截取函数)

介绍

字符串是在编程中经常使用的数据类型之一。C语言提供了一系列的函数,可以用于对字符串进行处理。这些函数被称作字符串函数。字符串函数允许我们对字符串进行复制、连接、比较、查找、处理等操作。

常用C语言字符串函数

- strlen(string):获取字符串的长度。

- strcpy(destination, source):将一个字符串复制到另一个字符串。

c语言字符串函数有哪些(c语言字符串截取函数)

- strcat(string1, string2):将一个字符串连接到另一个字符串的末尾。

- strcmp(string1, string2):比较两个字符串是否相同。如果字符串相同,函数返回0。

- strstr(string1, string2):在字符串1中查找字符串2,并返回一个指向找到的字符串的指针。

- strtok(string, delimiters):从字符串中提取标记。

- atoi(string):将字符串转换成整数。

实例展示

下面是一段示例代码,展示了如何使用C语言字符串函数:

#include <stdio.h>
#include <string.h>

int main() {
   char str1[50] = "Hello";
   char str2[30] = "World";
   char str3[80];

   printf("Length of str1: %dn", strlen(str1));
   printf("Copying str1 to str3...n");
   strcpy(str3, str1);
   printf("Str3: %sn", str3);
   printf("Concatenating str2 to str3...n");
   strcat(str3, str2);
   printf("Str3: %sn", str3);
   printf("Comparing str1 and str3...n");
   printf("Result of strcmp: %dn", strcmp(str1, str3));
   printf("Finding 'World' in str3...n");
   printf("Result of strstr: %sn", strstr(str3, "World"));
   printf("Tokenizing str3...n");
   
   char* token = strtok(str3, " ");
   while (token != NULL) {
      printf("%sn", token);
      token = strtok(NULL, " ");
   }
   
   printf("Converting '1234' to integer...n");
   int number = atoi("1234");
   printf("Number: %dn", number);
   
   return 0;
}

输出:

Length of str1: 5
Copying str1 to str3...
Str3: Hello
Concatenating str2 to str3...
Str3: HelloWorld
Comparing str1 and str3...
Result of strcmp: -72
Finding 'World' in str3...
Result of strstr: World
Tokenizing str3...
HelloWorld
Converting '1234' to integer...
Number: 1234

这段代码演示了strlen、strcpy、strcat、strcmp、strstr、strtok和atoi字符串函数的使用。请注意,每个函数都有不同的用途和语法。

本文来自投稿,不代表亲测学习网立场,如若转载,请注明出处:https://www.qince.net/cppel7.html

郑重声明:

本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。 若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。

我们不承担任何技术及版权问题,且不对任何资源负法律责任。

如遇到资源无法下载,请点击这里失效报错。失效报错提交后记得查看你的留言信息,24小时之内反馈信息。

如有侵犯您的版权,请给我们私信,我们会尽快处理,并诚恳的向你道歉!

(0)
上一篇 2023年4月16日 下午6:32
下一篇 2023年4月16日 下午6:32

猜你喜欢