c语言操作字符串

c语言不用预编译手写操作字符串

c语言中对字符串的 拼接,比较,大小写转换,长度

#include<stdio.h>
#include<string.h> 
int main() {
//************* 
int length(char *s);
void cat (char *s,char *b);
void upcase(char *p);
int cmp(char *s,char *b);
//*************
char s[81]={'1','2','3'};
char b[10] = "abcdXYZ";
//
int a = 0; 
printf("字符串'A'中内容如下\n");
while(s[a] != '\0'){
printf("%c",s[a]);
a ++;
}
printf("\n");
printf("字符串'X'中内容如下\n");
int x = 0; 
while(b[x] != '\0'){
printf("%c",b[x]);
x++;
}
//拼接 
cat(s,b);
int cs = 0 ;
printf("\n");
printf("字符串拼接打印\n");
while(s[cs] != '\0'){
printf("%c",s[cs]);
cs ++;
}
//长度 
int len;
len = length(s);
printf("\n");
printf("长度打印\n");
printf("%d",len);
printf("\n");
//大小写 
upcase(s);
int css = 0 ;
printf("大小写开始打印\n");
while(s[css] != '\0'){
printf("%c",s[css]);
css ++;
}
printf("\n");
//第几位不同 
int cmps;
char *sscmp = "12345";
char *sccmp = "123";
cmps = cmp(sscmp,sccmp);
printf("第一个数组如下\n");
puts(sscmp);
printf("第二个数组如下\n"); 
puts(sccmp);
printf("第%d位不同\n",cmps);
return 0;
}
int cmp(char *s,char *b) {
	//位数比较 
	int i = 1;
	while(*s == *b){
		i++;
		s++;
		b++;
	}
	return i;
} 
void cat(char *s,char *b){
	//字符串拼接 
		int i = 0;
		int c = 0;
		while(s[i]!='\0'){
			i++;
			if(s[i] == '\0'){
				while(b[c] != '\0'){
					s[i] = b[c];
					c++;
					i++;
				}
			}
		}
}
int length(char *p) {
	//字符串长度 
	int i = 0;
	while (*p!='\0'){
	i++;
	*p++;
	}
	return i;
}
void upcase(char *p){
	//大小写转换 
	while(*p!='\0'){
		if( *p >= 'a' && *p <= 'z') {
			*p-= 32;
		}
		else if( *p >= 'A' && *p <= 'Z') {
			*p+= 32;
		}
			p++;
	}
}
posted @ 2021-05-02 18:49  小王只会写bug  阅读(76)  评论(0编辑  收藏  举报