c语言实验报告(2)

实验二(1):

#include<stdio.h>
#include<string.h>

int main(){
	int i = 0;
	char s[80] = {" "},max[80]={" "}
	printf("请输入五个字符串:\n");
	scanf("%s",s);
	strcpy(max,s);
	for(int i = 1;i<5;i++){
		scanf("%s",s);
		if(strcmp(max,s)<0)
			strcmp(max,s);
	}
	printf("最大的字符串是:%s \n",max);
	return 0;
}

实验二(2):

#include<stdio.h>
#include<string.h>

void delchar(char *s,char ch)
{
	char *p;
	for(p = s;*s!='\0';s++)
	if(*s != ch)*p++=*s;
	*p = '\0';
}

signed main(){
	char str[1024],ch;
	printf("请输入主字符串:");gets(str);
	printf("请输入要删除的字符串:");
	scanf("%c",&ch);
	delchar(str,ch);
	printf("删除后的字符串是%s\n",str);
	return 0;
}

实验二(3):

#include<stdio.h>
#include<string.h>

void trmcpy(char *s,char *t,int m){
	int num = 0;
	for(int i = m-1;*(t+i)!='\0';i++){
		*(s++) = *(t+i);
	}
	return;
}



signed main(){
	char t[1024];
	char s[1024];
	int m;
	printf("请输入主字符串:");
	gets(t);
	scanf("%d",&m);
	trmcpy(s,t,m);
	printf("复制后的字符串为:%s\n",s);
	
	return 0;	
}

实验二(4):

#include<stdio.h>
//#include<iostream>
#include<string.h>
//using namespace std;


bool check(char *s,int len){
	
	for(int i = 0;i<len;i++){
	//	cout<< *(s+i)<<" "<< *(s+(len-i-1)) <<"\n";
		if(*(s+i)!=*(s+(len-i-1)))return 0;	
	}
	
	
	return 1;
}


signed main(){
	char s[1024];
	
	scanf("%s",s);
	int len = strlen(s);

	
	
	if(check(s,len))
	printf("Yes\n");
	else
	printf("No\n");
	
	
}

实验二(5):

#include <stdio.h>


void countCharacters(const char *str, int *uppercase, int *lowercase, int *spaces, int *digits, int *others) {

    while (*str) {

        if (*str >= 'A' && *str <= 'Z') {

            (*uppercase)++;

        } else if (*str >= 'a' && *str <= 'z') {

            (*lowercase)++;

        } else if (*str == ' ') {

            (*spaces)++;

        } else if (*str >= '0' && *str <= '9') {

            (*digits)++;

        } else {

            (*others)++;

        }

        str++;

    }

}


int main() {

    char input[100];

    printf("请输入一行字符:");

    fgets(input, sizeof(input), stdin);


    int uppercase = 0, lowercase = 0, spaces = 0, digits = 0, others = 0;

    countCharacters(input, &uppercase, &lowercase, &spaces, &digits, &others);


    printf("大写字母个数:%d\n", uppercase);

    printf("小写字母个数:%d\n", lowercase);

    printf("空格个数:%d\n", spaces);

    printf("数字个数:%d\n", digits);

    printf("其他字符个数:%d\n", others);


    return 0;

}

实验二:改错题:

#include <stdio.h>

#include <string.h>


int main() {

    char str1[100], str2[100];


    // 提示用户输入两个字符串

    printf("请输入第一个字符串: ");

    scanf("%s", str1);


    printf("请输入第二个字符串: ");

    scanf("%s", str2);


    // 计算第一个字符串的长度

在这里插入代码片

    int len1 = strlen(str1);


    // 连接第二个字符串到第一个字符串的末尾

    int i, j = 0;

    for (i = len1; str2[j] != '\0'; i++) {

        str1[i] = str2[j];

        j++;

    }

    str1[i] = '\0'; // 手动添加字符串结尾


    // 输出连接后的字符串

    printf("连接后的字符串: %s\n", str1);


    return 0;

}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值