字符串的几道题目及其解答

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

int main() {
    char str[81]; // 假设字符串最大长度为80,加上终止符'\0'
    
    // 输入字符串
    fgets(str, sizeof(str), stdin);

    // 计算字符串长度
    size_t len = strlen(str);

    // 去掉输入字符串末尾的换行符
    if (len > 0 && str[len - 1] == '\n') {
        str[len - 1] = '\0';
        len--;
    }

    // 去重
    for (size_t i = 0; i < len; i++) {
        for (size_t j = i + 1; j < len; ) {
            if (str[i] == str[j]) {
                // 如果发现重复字符,将后面的字符向前移动
                for (size_t k = j; k < len; k++) {
                    str[k] = str[k + 1];
                }
                len--; // 减少字符串长度
            } else {
                j++; // 继续比较下一个字符
            }
        }
    }

    // 排序
    for (size_t i = 0; i < len - 1; i++) {
        for (size_t j = 0; j < len - 1 - i; j++) {
            if (str[j] > str[j + 1]) {
                // 交换字符
                char temp = str[j];
                str[j] = str[j + 1];
                str[j + 1] = temp;
            }
        }
    }

    // 输出结果
    printf("%s\n", str);

    return 0;
}

 

#include <iostream>
#include <string> 
using namespace std;
int main() 
{
	string c;
	getline(cin, c);
	string s1="6666";//从四个6开始查找
	int j,n=0,q;
	q=j=c.find(s1);//寻找6的个数大于等于4的字符串首位位置 
	while(q<c.length())//多次查找替换 
	{
		while(c[j++]=='6')
			n++;
		if(n>9)
		{ 
			c.replace(q,n,"27");//替换 
			q=j=c.find(s1,q+2); //从7后第一个字符开始查找,提高效率 
		} 
		else
		{ 
			c.replace(q,n,"9");
			q=j=c.find(s1,q+1); //同理 
		} 
		n=0;//置零,为下一次计数准备 
	}
	cout<<c<<endl;		
	return 0;
}

 

 

#include <iostream>
#include <string>
using namespace std;
int main() {
    string message;
    int totalMessages = 0;
    int firstOccurrence = -1;
    int count = 0;
    while (getline(cin, message)) {
        if (message == ".") {
            break; // 输入结束
        }
        totalMessages++;
        if (message.find("chi1 huo3 guo1") != string::npos) {
            count++;
            if (firstOccurrence == -1) {
                firstOccurrence = totalMessages;
            }
        }
    }
    cout << totalMessages << endl;
    if (count > 0) {
        cout << firstOccurrence << " " << count << endl;
    } else {
        cout << "-_-#" << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

残念亦需沉淀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值