字符串
        【摘要】 字符串的输入输出以及重点的字符串函数使用和相同功能自定义函数的编写
    
    
    
    1. 单字符输入输出
putchar
- int putchar(int c)
 - 向标准输出写一个字符
 - 返回写了几个字符,EOF(-1)表示失败
getchar - int getchar(void)
 - 从标准输入读入一个字符
 - 返回类型是int是为了返回EOF(-1)
 - 结束 
   
- windows->Ctrl+z
 - unix->Ctrl+d
 
 
#include <stdio.h>
int main(int argc, char const *argv[]) {
	int ch;
	while ((ch = getchar()) != EOF) {
		putchar(ch);
	}
	printf("EOF\n");
	return 0;
}
 
 这个程序在运行的时候,任何值包括EOF都能输出,在输出Ctrl+D的时候,程序输出EOF,并结束了。
 原因:(键盘+显示器)————>shell————>程序。
 shell:行编辑的功能。
 键盘上输入的东西在回车之前,这些字符都没有送到程序那里去,都在shell那里。
2. 字符串数组
- char **a 
   
- a是一个指针,指向另一个指针,那个指针指向一个字符(串)
 
 - char a[][] 
   
- 第二维必须有确切的大小
 - 字符串的长度不能超过第二维的大小
 
 
#include <stdio.h>
int main(void) {
	char a[][10] = {
		"hello",
                //"sasdsfgafsghuvnihdjnk"//错误
	};
	return 0;
}
 
 a[][10]:
 a[0]-- > char [10]
 a[0]-- > char *

3. 程序参数
- int main(int argc,char const *argv[])
 - argc是argv的长度
 - argv[0]是命令本身 
   
- 当使用unix的符号链接时,反映符号链接的名字
 
 
#include <stdio.h>
int main(int argc, char const *argv[]) {
	int i;
	for (i = 0; i < argc; i++) {
		printf("%d:%s\n", i, argv[i]);
	}
	return 0;
}
 
 busybox:Linux工具箱
4. 函数strlen
string.h
- #include<string.h>
 - strlen 
   
- size_t strlen(const char *s)
 - 返回s的字符串长度(不包括结尾的0)
 - const 不修改字符串本身
 
 - strcmp
 - strcpy
 - strcat
 - strchr
 - strstr
 
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[]) {
	char line[] = "hello";
	printf("strlen=%lu\n", strlen(line));
	printf("sizeof=%lu\n", sizeof(line));
	return 0;
}
 
 写一个函数实现strlen的功能:
#include <stdio.h>
int mylen(const char *s) {
	int idx = 0;
	while (s[idx] != '\0') {
		idx++;
	}
	return idx;
}
int main(int argc, char const *argv[]) {
	char line[] = "hello";
	printf("%d\n", mylen(line));
	return 0;
}
 
 5. 函数strcmp
- int strcmp(const chars1,const chars2)
 - const:在比较的过程中不修改字符串本身
 - 比较两个字符串,字符都相等,比较到‘\0’也相等,则返回0
 - 比较两个字符串,返回:
 - 0:s1==s2
 - 1:s1>s2
 - -1:s1<s2
 - 如果s1=(abc),s2=(Abc),srccmp返回的结果是32,是’a’-'A’的差值
 - 如果s1=(abc),s2=(Abc‘空格’),srccmp返回的结果是-32,‘\0’的ASCII码是0,‘\0’的ASCII码是32,是用‘\0’-‘空格’的差值。
注意:不能用**==**比较两个数组,这样比较只能比较他们的地址,因为数组的地址是不可能相等的。 
第一种:用while把if里的语句写到一起了:
#include <stdio.h>
int mycmp(const char *s1, const char *s2) {
	int idx = 0;
	while (s1[idx] == s2[idx] && s1[idx] != '\0') {
//		if(s1[idx] != s2[idx]) {
//			break;
//		} else if(s1[idx]=='\0') {
//			break;
//		}
		idx++;
	}
	return s1[idx] - s2[idx];
}
int main(int argc, char const *argv[]) {
	char line1[] = "hello";
	char line2[] = "hello";
	printf("%d\n", mycmp(line1, line2));
	return 0;
}
 
 第二种:把idx去掉了
int mycmp(const char *s1, const char *s2) {
	int idx = 0;
	while (*s1 == *s2 && *s1 != '\0') {
		*s1++;
		*s2++;
	}
	return *s1 - *s2;
}
 
 喜欢第二种,看上去更简洁
            【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
                cloudbbs@huaweicloud.com
                
            
        
        
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)