【c/c++编程-字符问题】字符移动、字母统计、首字母大写、单词统计、加密算法

目录

字符移动

字母统计

首字母大写

单词统计 

加密算法


字符移动

题目描述

输入一个字符串,将其中的数字字符移动到非数字字符之后,并保持数字字符和非数字字符输入时的顺序。例如:输入字符串“ab4f35gr#a6”,输出为“abfgr#a4356”。

输入描述:

输入一行字符串,长度小于100。

输出描述:

输出结果。

输入样例#:

ab4f35gr#a6
abfgr#a4356

思路:

移动字符:先输出非数字字符,再输出数字字符

分别定义存放字符和存放数字的数组,再通过for遍历gets获得的字符串,如果是非数字字符则输出字符数组,再将数字字符保存在digits数组中,遍历digits数组下标打印。

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main() {
	char s[105];//字符 
	char digits[105];//数字 
	int len  = strlen(s);
	int cnt = 0;//数组下标 
    gets(s); 
	
	for (int i = 0;i<len;i++){
		if(s[i]<='0'||s[i]>='9'){
			cout<<s[i]; 
			}
		else{
			digits[cnt++] = s[i];
			}
		}
		for(int i = 0;i<cnt;i++)
			cout<<digits[i];
	return 0;
}

字母统计

题目描述

输入一行字符串,计算其中A-Z大写字母出现的次数

输入描述:

案例可能有多组,每个案例输入为一行字符串。

输出描述:

对每个案例按A-Z的顺序输出其中大写字母出现的次数。

输入样例#:

DFJEIWFNQLEF0395823048+_+JDLSFJDLSJFKK

输出样例#:

A:0
B:0
C:0
D:3
E:2
F:5
G:0
H:0
I:1
J:4
K:2
L:3
M:0
N:1
O:0
P:0
Q:1
R:0
S:2
T:0
U:0
V:0
W:1
X:0
Y:0
Z:0

思路:用count统计函数 ,遍历获得的字符串进行统计,输出 

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

int main() 
{
	string s;
	int count[26] = {0};//初始化count函数,统计A-Z出现的次数 
	
	while(getline(cin,s))
	{
		for(int i = 0;i<s.length();i++)
		{
			if(s[i]>='A'&&s[i]<='Z')
				count[i-'A']++;//ASCII码的处理 
				}
	
		for(char c = 'A';c<='Z';c++) 
		cout<<c<<':'<<count[c-'A']<<endl;
		
}
	return 0;
}

首字母大写

题目描述

对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。 在字符串中,单词之间通过空白符分隔,空白符包括:空格(' ')、制表符('\t')、回车符('\r')、换行符('\n')。

输入描述:

输入一行:待处理的字符串(长度小于100)。

输出描述:

可能有多组测试数据,对于每组数据,
输出一行:转换后的字符串。

输入样例#:

if so, you already have a google account. you can sign in on the right.

输出样例#:

If So, You Already Have A Google Account. You Can Sign In On The Right.
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

int main() {
	char s[100];//字符数组
	gets(s);//输入
	
//遍历每个字符i,i不为空格
	for (int i = 0;i!= '\0';i++)
	{
//确定单词首字母:首字母前面为空格
		if(s[i] = ' ' && s[i+1] != ' ')
		{
//首字母是否为小写字母
			if(islower(s[i+1])) 
//转换为大写字母
			s[i + 1]  = toupper(s[i + 1]);
		}
	}
//句子中第一个字母
	if (islower(s[0]))
		s[0] = toupper(s[0]);
	puts(s);
	return 0;
}

单词统计 

题目描述:

编一个程序,读入用户输入的,以“.”结尾的一行文字,统计一共有多少个单词,并分别输出每个单词含有多少个字符。
(凡是以一个或多个空格隔开的部分就为一个单词)

输入: 

输入包括1行字符串,以“.”结束,字符串中包含多个单词,单词之间以一个或多个空格隔开。

输出:

可能有多组测试数据,对于每组数据,
输出字符串中每个单词包含的字母的个数。

样例输入: 

hello how are you.
1

样例输出: 

5 3 3 3 

  • 用数组。读取的字符串天然以空格分割,而且不在乎空格的个数。需要判断最后的一个'.'是否存在。
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

int main() {
//	当用scanf函数输入字符串时,字符串中不能含有空格,否则将以空格作为串的结束符。
	char temp[100];
	while (scanf("%s", temp) != EOF) {//C语言中,EOF常被作为文件结束的标志
		int count = 0;
		int len = strlen(temp);//字符串长度 
	if (temp[len - 1] == '.') 
		{
		count = len - 1;
		printf("%d\n", count);
		} 
	else 
		{
		count = len;
		printf("%d ", count);
	}
}
		return 0;
}

加密算法

题目描述: 

编写加密程序,加密规则为:将所有字母转化为该字母后的第三个字母,即 A->D、B->E、 C->F、......、Y->B、Z->C。小写字母同上,其他字符不做转化。输入任意字符串,输出加密 后的结果。 例如:输入"I love 007",输出"L oryh 007"

输入描述:

输入一行字符串,长度小于 100。

输出描述: 

输出加密之后的结果。

输入样例#: 

I love 007

输出样例#:

L oryh 00  

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

int main() {
	char s[105];
	gets(s);//输入一行文本用gets 
	int len = strlen(s);
	
	for (int i = 0;i<len;i++){
		if(s[i]>='A'&& s[i]<='Z'){
			s[i] += 3;
		}
			if (s[i]>'Z') s[i]-= 26;
		else if(s[i]>='a'&& s[i]<='z'){
			s[i] += 3;
			if (s[i]>'z') s[i]-= 26;
		}
		else{
			continue;
		}
	}
	puts(s);
	return 0;	
	
}

			

猜你喜欢

转载自blog.csdn.net/m0_51933492/article/details/127103141
今日推荐