如何设置单词第一个字母大写
Problem statement:
问题陈述:
Given an input line, capitalize first and last letter of each word in the given line. It's provided that the input line is in lowercase.
给定输入行, 将给定行中每个单词的第一个和最后一个字母大写 。 假设输入行是小写的。
Solution:
解:
The basic algorithm is to keep track of the spaces and to capitalize the letter before space & after space. Also, the first letter and the last letter of the line should be capitalized.
基本算法是跟踪空格并在空格之前和之后大写字母。 另外,该行的第一个字母和最后一个字母应大写。
Few more things that need to be kept in mind such that:
需要记住的其他几件事:
More than one occurrence of space in between two words.
两个单词之间出现多个空格。
There may be word of single letter like 'a', which need to be capitalized.
可能有单个字母的单词,例如'a' ,需要大写。
There may be word of two letters like 'me', where both the letters need to be capitalized.
可能有两个字母的单词,例如“ me” ,其中两个字母都必须大写。
Algorithm:
算法:
Create a hash table.
创建一个哈希表。
Insert index of first letter, i.e., 0 & the index of last letter, i.e., length-1. length be the length of input line.
插入第一个字母的索引,即0和最后一个字母的索引,即length-1 。 length是输入线的长度。
For i=0:length-1 Find index of spaces in the line If index before spaces are not in hash table Insert into hash table If index after spaces are not in hash table Insert into hash table
Capitalize the indexes present in the hash table
大写哈希表中存在的索引
line [index]-=32;
行[index]-= 32;
//ASCII value of lower case letter - ASCII value of corresponding upper case letter=32
//小写字母的ASCII值 -相应大写字母的ASCII值 = 32
Print the converted input line
打印转换后的输入行
Inclusion of hash table in the program helps us to avoid inserting duplicate indexes. Otherwise, corner test-cases may fail.
在程序中包含哈希表有助于我们避免插入重复的索引。 否则,角落测试用例可能会失败。
C ++程序将一行中每个单词的首字母和最后一个字母大写 (C++ program to capitalize first and last letter of each word in a line)
#include <bits/stdc++.h>
using namespace std;
void capitalize(char* arr,int i){
//ascii value of each lower case letter-ascii value
//of each uppercase letter=32
//i is the length of line
unordered_set<int> table;
table.insert(0); //index of first letter of line
table.insert(i-1);//index of last letter of line
for(int j=1;j<i;j++){
if(arr[j]==' '){
// last letter of word is before
//space & first letter of word is after space
//check index already present in hash table or not
if(table.find(j-1)==table.end())
table.insert(j-1); //if not insert index
//check index already present in hash table or not
if(table.find(j+1)==table.end())
table.insert(j+1); //if not insert index
}
}
//capitalize
for(auto it=table.begin();it!=table.end();it++)
arr[*it]-=32;
printf("converted input line is: ");
//printing
for(int j=0;j<i;j++)
printf("%c",arr[j]);
printf("\n");
}
int main(){
//store the input line
char arr[100];
char c;
int i=0;
printf("input the line.....\n");
scanf("%c",&c);
while(c!='\n'){
arr[i++]=c;
scanf("%c",&c);
}
capitalize(arr,i);
return 0;
}
Output
输出量
First run:
input the line.....
hello world
converted input line is: HellO WorlD
Second run:
input the line.....
includehelp is a great paltform for geeks
converted input line is: IncludehelP IS A GreaT PaltforM FoR GeekS
Recommended posts
推荐的帖子
Run-length encoding (find/print frequency of letters in a string)
游程编码(字符串中字母的查找/打印频率)
Sort an array of 0's, 1's and 2's in linear time complexity
以线性时间复杂度对0、1和2的数组进行排序
Finding subarray with given sum
查找给定总和的子数组
1[0]1 Pattern Count
1 [0] 1个样式计数
Greedy Strategy to solve major algorithm problems
解决主要算法问题的贪婪策略
Job sequencing problem
工作排序问题
Exit Point in a Matrix
矩阵中的出口点
Generate Gray Code Sequences
生成格雷码序列
Picking Numbers
领料号码
Run-length encoding (find/print frequency of letters in a string)
游程编码(字符串中字母的查找/打印频率)
Count and Say sequence
计数并说出顺序
Longest Common Prefix
最长的公共前缀
Count Substrings
计数子串
Number following the pattern
跟随模式的数字
Next Permutation
下一个排列
翻译自: https://www.includehelp.com/icp/capitalize-first-and-last-letter-of-each-word-in-a-line.aspx
如何设置单词第一个字母大写