[C/C++ Programming-Character Problems] Character movement, letter statistics, initial capitalization, word statistics, encryption algorithm

Table of contents

character movement

Letter statistics

Capitalize first letter

word statistics 

Encryption Algorithm


character movement

Question description

Enter a string in which the numeric characters are moved after the non-numeric characters, keeping the order in which the numeric and non-numeric characters are entered. For example: input the string "ab4f35gr#a6", the output is "abfgr#a4356".

Enter description:

Enter a line of string, the length is less than 100.

Output description:

Output results.

Enter sample #:

ab4f35gr#a6
abfgr#a4356

Idea:

Move characters: output non-numeric characters first, then output numeric characters

Define the arrays that store characters and numbers respectively, and then use for to traverse the string obtained by gets. If it is a non-numeric character, output the character array, then save the numeric characters in the digits array, and traverse the digits array subscript to print.

#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;
}

Letter statistics

Question description

Enter a line of string and count the number of times the uppercase letters A-Z appear in it.

Enter description:

There may be multiple groups of cases, and each case is input as a line of strings.

Output description:

For each case, output the number of occurrences of uppercase letters in the order A-Z.

Enter sample #:

DFJEIWFNQLEF0395823048+_+JDLSFJDLSJFKK

Output sample#:

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

Idea: Use the count statistical function to traverse the obtained strings for statistics and output 

#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;
}

Capitalize first letter

Question description

For all words in a string, if the first letter of the word is not a capital letter, change the first letter of the word to a capital letter. In a string, words are separated by whitespace characters. Whitespace characters include: spaces (' '), tabs ('\t'), and carriage returns ( 39;\r'), newline character ('\n').

Enter description:

Enter a line: the string to be processed (length less than 100).

Output description:

There may be multiple sets of test data. For each set of data,
Output one line: the converted string. 

Enter sample #:

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

Output sample#:

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;
}

word statistics 

Topic description:

Write a program that reads a line of text input by the user that ends with ".", counts how many words there are in total, and outputs how many characters each word contains.
(Any part separated by one or more spaces is a word)

Input: 

The input includes 1 lineString, ending with ".", the string contains multiple words, and the words are separated by an or Separated by multiple spaces.

Output:

There may be multiple sets of test data. For each set of data,
outputs the number of letters contained in each word in the string.

Sample input: 

hello how are you.
1

Sample output: 

5 3 3 3 

  • Use an array. The read string is naturally separated by spaces, and the number of spaces does not matter. It is necessary to determine whether the last '.' exists.
#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;
}

Encryption Algorithm

Title description: 

Write an encryption program. The encryption rules are: convert all letters into the third letter after the letter, that is, A->D, B->E, C->F,..., Y- >B, Z->C. Lowercase letters are the same as above, other characters are not converted. Enter any string and output the encrypted result. For example: input "I love 007", output "L oryh 007"

Enter description:

Enter a line of string, less than 100 characters in length.

Output description: 

Output the encrypted result.

Enter sample#: 

I love 007

Output sample#:

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;	
	
}

			

Guess you like

Origin blog.csdn.net/m0_51933492/article/details/127103141