如何設置單詞第一個字母大寫
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
如何設置單詞第一個字母大寫