day07C基础作业

  1. 从终端获取一个字符串,要求将该字符串倒置存储。不允许使用辅助数组
  2. 从终端获取一个字符串,判断该字符串中小写字母分别有几个。aabbbcc --->a2 b3个 c有2个
  3. 字符串有效长度函数重写strlen()
  4. 字符串拷贝函数重写strcpy()
  5. 字符串比较函数重写strcmp()
  6. 字符串拼接函数重写strcat()
  7. 从终端输入一个字符串,要求删除字符串中的空格字符,请在原字符串上操作,不允许使用辅助数组

1. 

#include<stdio.h>
#include<string.h>
int main(){
	char a[110];
	gets(a);
	int i,con,j;
	con = strlen(a);
	for(i = con-1,j = 0;i > j ;i--,j++){
		char d;
		d = a[j];
		a[j] = a[i];
		a[i] = d;
	}
	printf("%s",a);
	return 0;
}

2.

#include<stdio.h>
#include<math.h>
int main(){
	char a[110];
	char b[110];
	char s[25];
	int arr[110]={0};
	int con = 0,j = 0;
	printf("输入一个字符串:\n");
	gets(a);
	for(int i = 0; a[i]!= '\0'; i++){
		if(a[i] >= 'a' && a[i] <= 'z'){
			b[j++] = a [i];
			con++;
		}
	}
	for(int i = 0;i< 26;i++)
		s[i]=i+97; 
	
	for(int i = 0;i < 26; i++){
		int flag = 0;
		for(int j = 0;j<con;j++){
			if(b[j] == s[i])
				flag ++;
		}
		arr[i] = flag;
	}
	for(int i = 0;i<= 26;i++)
		if(arr[i]!= 0)
			printf("%c有%d个\n",s[i],arr[i]); 
			
	printf("小写字母为%s,有%d个",b,con);
	return 0;
}

3.

#include<stdio.h>
const int N = 110;
int main(){
	char arr[N] = {0}; 
	int con;
	gets(arr);
	for(int i = 0;arr[i] != '\0'; i++)
		con++;
	printf("有效长度为:%d",con); 
	return 0;
}

4.

#include<stdio.h>
#include<string.h>
const int N = 110;
int main(){
	char str1[N] = {"21434fgrgyd"};
	char str2[N] = {"afa f234\0fefG"};
	int con = 0;
	for(int i = 0; str2[i] != 0; i++)
		con++;
	for(int i = 0; i <= con; i++)
		str1[i] = str2[i];
	
	for(int i = 0; i <= N; i++)
		printf("%c",str1[i]);
	return 0;
}

5.

#include<stdio.h>
#include<string.h>
const int N = 110;
int main(){
	char str1[N] = {"abc34fgrgyd"};
	char str2[N] = {"aba f234\0fefG"};
	int s1 = strlen(str1);
	int s2 = strlen(str2);
	if(s1 < s2)
		s1 = s2;
	for(int i = 0; i < s1; i++)
		if(str1[i] == str2[i]);
		else{
			printf("%d",str1[i]-str2[i]);
			break;
		}		
	return 0;
}

6.

#include<stdio.h>
#include<string.h>
const int N = 110;
int main(){
	char str1[N]={0};//初始化 
	char str2[N]={0};
	printf("输入两个字符串:\n");
	printf("第一个:\n");
	gets(str1);
	printf("第二个:\n"); 
	gets(str2);
	int s1 = strlen(str1);
	int s2 = strlen(str2);
	int res = strcmp(str1,str2);
	int sum = s1 + s2;
	if(s1 == s2){
		if(res< 0){
			for(int i = s1; i < sum; i++)
				str1[i] = str2[i-s1];
			printf("%s",str1);
		}
		else{
			for(int i = s2; i < s1+s2; i++)
				str2[i] = str1[i-s2];
			printf("%s\n",str2);
		}
	}
	else{
		if(res> 0){
			for(int i = s1; i < s1+s2; i++)
				str1[i] = str2[i-s1];		
			printf("%s\n",str1);

		}
		else{
			for(int i = s2; i < s1+s2; i++)
				str2[i] = str1[i-s2];	
			printf("3%s\n",str2);		
		}
	}			
	return 0;
}

7.

#include<stdio.h>
#include<string.h>
const int N = 110;
int main(){
	char arr[N] = {0}; 
	gets(arr);
	int s = strlen(arr),con = 0;
	for(int i = 0;i < s+con; i++)
		if(arr[i+con] == ' ')
		{
			con++;
			while(arr[i + con] == ' '){//消除空格 
				con++;
			}
			arr[i] = arr[i+con];
		}
		else{
			arr[i] = arr[i+con];
		}
			
	printf("%s",arr); 
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值