程序员的知识教程库

网站首页 > 教程分享 正文

C语言笔记-0605字符串数组和函数(c语言,字符串数组)

henian88 2024-10-25 11:54:25 教程分享 2 ℃ 0 评论

1,字符串函数

字符串中函数的中出现n的地方都是从来限制长度

字符串长度获取:strlen


#include <stdio.h>

#include <string.h>


int main()

{

int i=0; char str[]="hello"; char str1[]={'h','e','l','l','o'};

printf("sizeof(str)=%lu\n",sizeof(str));

printf("sizeof(str1)=%lu\n",sizeof(str1));

printf("strlen(str)=%lu\n",strlen(str));

printf("strlen(str1)=%lu\n",strlen(str1));

return 0;

}

strnlen - determine the length of a fixed-size string #include <string.h> size_t strnlen(const char *s, size_t maxlen); size_t :接收获取到的字符长度 const char *s:字符数组的首地址,即数组的名字 size_t maxlen:最大长度,用来指定获取字符串的长度,不能超过字符长度


字符串的拼接:

strcat:

strcat, strncat - concatenate two strings SYNOPSIS #include <string.h> char *strcat(char *dest, const char *src); char *:strcat的返回值,返回的是strcat函数拼接后字符串的首地址 char *dest:目标字符串,指的是被拼接到的字符串的名字 const char *src:源字符串,是待拼接的字符串 char *strncat(char *dest, const char *src, size_t n); size_t n:指的是拼接n个字符

#include <stdlib.h>

#include <string.h>

int main()

{

int i=0; char str[15]="hello";

char buf[6]="world";

printf("%s\n",strcat(str,buf));

printf("%s\n",strncat(str,buf,2));

return 0;

}

字符串的比较:

strcmp, strncmp - compare two strings #include <string.h> int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t n); strcmp:比较字符串是否相同 size_t n:指定比较的字节数 int:整型返回值,用来表示s1>s2(>0) 或者 s1=s2(=0)或者 s1<s2(0<) 注意:比较字符串的函数strcmp比较的是两个字符串在ASCII中的位置对应的值,若在 ASCII中的位置靠后说明值比较大,只有两个字符完全相同的时候才相同,如两个字符的 开头相同会继续向后进行比较直到比较出大小

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <time.h>

int main()

{

int ret=0; char str[15]="hellp"; char buf[6]="hello";

ret=strcmp(str,buf);

if(ret>0) {

printf("str>buf\n"); }

else if(ret==0) printf("str=buf\n");

else printf("str<buf\n");

printf("%d\n",strncmp(str,buf,4)); return 0; }

字符串的拷贝:

strcpy:string copy

strcpy, strncpy - copy a string #include <string.h> char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n); dest:目标字符串 src:源字符串 strcpy:将源字符串拷贝到目标字符串 strncpy:将源字符串的前n个拷贝到目标字符串,只是覆盖目标字符串的前n个字节

#include <stdlib.h>

#include <string.h>

#include <time.h>

int main()

{ int ret=0; char str[15]="hello"; char buf[6]="world";

// printf("%s\n",strcpy(str,buf));

printf("%s\n",strncpy(str,buf,4)); return 0; }

字符串的获取函数:

gets:

gets - get a string from standard input (DEPRECATED) #include <stdio.h> char *gets(char *s); char *s:是保存从标准输入设备(键盘)获取到字符串的首地址,字符数组的名字 char *:返回接收到字符串的首地址

char *fgets(char *s, int size, FILE *stream); char *s: 是保存从标准输入设备(键盘)获取到字符串的首地址,字符数组的名字 int size: 数组的大小 FILE *stream:输入文件流,我们当前使用的stdin char*:返回首地址 在嵌入式开发中存在三个标准设备:

设备流 对应标识符 对应设备

标准输入流: stdin 键盘

标准输出流: stdout 显示器

标准错误流: stderr 显示器

字符串输出函数:

int puts(const char *s); const char *s:待输出的字符串的首地址

int fputs(const char *s, FILE *stream); const char *s:待输出的字符串的首地址 FILE *stream:输出到的文件流,我们用stdout

#include <stdlib.h> #include <string.h> #include <time.h> int main() { char buf[20]={0}; gets(buf); puts(buf); return 0; }

#include <stdlib.h> #include <string.h> #include <time.h> int main() { char buf[20]={0}; //gets(buf);//等同于scanf("%s",buf); //puts(buf);//等同于printf("%s",buf); fgets(buf,20,stdin); fputs(buf,stdout); return 0; }

字符的输入:

getchar:

int getchar(void); int:用来接收获取到的单个字符的ascii码

int getc(FILE *stream); int:用来接收获取到的单个字符的ascii码 FILE *stream:标准输入流stdin

字符输出:

putchar:

int fputc(int c, FILE *stream); int putchar(int c); int c:待输出的字符 FILE *stream:输出的目标流,此处是标准输出流stdout

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <time.h>

int main()

{

char ch=' '; char buf[20]={0};

//gets(buf);//等同于scanf("%s",buf);

//puts(buf);//等同于printf("%s",buf);

fgets(buf,20,stdin);

fputs(buf,stdout);

//ch=getchar(); //putchar(ch);

ch=fgetc(stdin);

fputc(ch,stdout);

printf("\n"); return 0;

}

提示:带有f的函数接口针对的输入输出流比较多,不带有f的接口只能对标准输入输出操作

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表