C语言第66+67+68课常用字符串处理函数

第66+67+68课 常用字符串处理函数

1、strlen函数		字符串长度



				//程序-------------------------------------------	
						#include <stdio.h>
						#include <stdlib.h>
						#include <string.h>
				
						int main()
						{
							char world1[] = "HelloWorld";
							char world2[] = {'H','e','l','l','e','W','o','r','l','d','\0'};
							char world3[] = "你好!北京大学!";
							printf("world1的长度为%d\n",strlen(world1));
							printf("world2的长度为%d\n",strlen(world2));
							printf("world3的长度为%d\n",strlen(world3));
						
							return 0;
						}
				//程序-------------------------------------------	
					
						
				//运行结果-------------------------------------------
				
						
							world1的长度为10
							world2的长度为10
							world3的长度为16
							
							Process returned 0 (0x0)   execution time : 1.482 s
							Press any key to continue.
				//运行结果-------------------------------------------
							
				
				
				
				//comment-------------------------------------------
				不算最后\0的长度,一个中文字符占据两个字节
				//comment-------------------------------------------
				
				
2、strcpy:		字符串的复制(每个元素逐个进行)
		
		
				//程序-------------------------------------------			
				#include <stdio.h>
				#include <stdlib.h>
				#include <string.h>
				
				int main()
				{
					char world1[50] = "HelloWorld";
					char world2[20];
					printf("请输入字符串:\n");
					scanf("%s", world2);//数组名本来就是一个地址,所以不需要“&”了
					//使用内置函数,将 world2中的内容复制到world1数组中去
					strcpy(world1,world2);
					printf("world1的值是%s\tworld2的值是%s\n",world1,world2);
					return 0;
				}
				//程序-------------------------------------------	
				
				
				//运行结果-------------------------------------------
				
				请输入字符串:
				abc
				world1的值是abc world2的值是abc
				
				Process returned 0 (0x0)   execution time : 3.540 s
				Press any key to continue.
				//运行结果-------------------------------------------
	


3、strcpy:		字符串比较
				相等返回0
				1 < 2 返回正数
				1 > 2 返回负数
				
				
				
				
				
				//用户名密码登录程序-------------------------------------------	
				#include <stdio.h>
				#include <stdlib.h>
				#include <string.h>
				
				#define USER_NAME   "admin"
				#define PASSWORD    "admin"
				
				/**
				*验证传入的用户名和密码是否正确
				*参数1:需要验证的用户名
				*参数2:需要验证的密码
				*返回:如果用户名和密码合法,返回1,否则,返回0
				*/
				int login(char [],char[]);
				
				int main()
				{
					char username[50];
					char password[50];
					printf("请输入用户名:");
					scanf("%s",username);
					printf("请输入密码:");
					scanf("%s",password);
					if(login(username,password) == 1)
					{
						printf("密码正确!\n");
					}
					else
					{
						printf("用户或密码错误,请重新输入!\n");
					}
				
					return 0;
				}
				
				int login(char username[], char password[])
				{
					int result = 0;
					if(strcmp(USER_NAME,username) == 0 && strcmp(PASSWORD,password) == 0)
					{
						//验证合法
						result = 1;
					}
					return result;
				}	
				//用户名密码登录程序-------------------------------------------	

				
				//运行结果-------------------------------------------
				请输入用户名:admin
				请输入密码:admin
				密码正确!
				
				Process returned 0 (0x0)   execution time : 6.014 s
				Press any key to continue.
				//运行结果-------------------------------------------
				
				
4strcat(str1, str2):		字符串连接
							将2号字符串拼接到1号字符串中去
							注意1号字符串的长度一定要大
							
							
				//字符串连接程序-------------------------------------------			
											
				#include <stdio.h>
				#include <stdlib.h>
				#include <string.h>
				
				
				
				int main()
				{
					//演示字符串连接
					char str1[100] = "你好,中国!";
					char str2[20] = "你也好,美国!";
					//字符串连接
					strcat(str1,str2);
					printf("str1 = %s\tstr2 = %s\n",str1,str2);
				
					return 0;
				}
				//字符串连接程序-------------------------------------------			
				
				
				//运行结果-------------------------------------------
				str1 = 你好,中国!你也好,美国!        str2 = 你也好,美国!
				
				Process returned 0 (0x0)   execution time : 1.897 s
				Press any key to continue.
				//运行结果-------------------------------------------



								

猜你喜欢

转载自blog.csdn.net/cxd15194119481/article/details/86554810