c字符串和字符串函数学习总结

xiaoxiao2021-02-28  101

1.字符串结尾的空字符'\0'的ascii码为0,故也可用

while(*str); 来判断字符串的结束。

2.函数作形参时,传递地址后是对地址进行操作,实参的值也会改变:

#include<stdio.h> void pass(char *p) { *p = 7; printf("%p and %d\n",p,*p); } int main(void) { char *ptr; char i = 5; ptr = &i; pass(ptr); printf("%p and %d\n",ptr,*ptr); return 0; } 其后的*ptr为7。

3.for()循环中判断条件为1时,无限循环;为0时,不循环;故可用isspace()函数来实现判断字符中空格读取单词等操作: 读取字符串中的第一个单词:

#include <stdio.h> #include<ctype.h> void word(char *p); int main(void) { char a[20]; puts("input your string"); gets(a); word(a); puts(a); return 0; } void word(char *p) { int begin,end; for(begin = 0;isspace(*(p + begin));begin++) continue; for(end = begin;!isspace(*(p + end));end ++) continue; *(p+end) = '\0'; for(;*(p + begin) != '\0';p++) *p = *(p + begin); *p = '\0'; }
转载请注明原文地址: https://www.6miu.com/read-63739.html

最新回复(0)