如何设置单词第一个字母大写_大写一行中每个单词的第一个和最后一个字母
如何設(shè)置單詞第一個字母大寫
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.
給定輸入行, 將給定行中每個單詞的第一個和最后一個字母大寫 。 假設(shè)輸入行是小寫的。
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.
基本算法是跟蹤空格并在空格之前和之后大寫字母。 另外,該行的第一個字母和最后一個字母應(yīng)大寫。
Few more things that need to be kept in mind such that:
需要記住的其他幾件事:
More than one occurrence of space in between two words.
兩個單詞之間出現(xiàn)多個空格。
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.
創(chuàng)建一個哈希表。
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是輸入線的長度。
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值 -相應(yīng)大寫字母的ASCII值 = 32
Print the converted input line
打印轉(zhuǎn)換后的輸入行
Inclusion of hash table in the program helps us to avoid inserting duplicate indexes. Otherwise, corner test-cases may fail.
在程序中包含哈希表有助于我們避免插入重復(fù)的索引。 否則,角落測試用例可能會失敗。
.minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}C ++程序?qū)⒁恍兄忻總€單詞的首字母和最后一個字母大寫 (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 lineunordered_set<int> table;table.insert(0); //index of first letter of linetable.insert(i-1);//index of last letter of linefor(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 notif(table.find(j-1)==table.end())table.insert(j-1); //if not insert index//check index already present in hash table or notif(table.find(j+1)==table.end()) table.insert(j+1); //if not insert index}}//capitalizefor(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 linechar 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 WorlDSecond run: input the line..... includehelp is a great paltform for geeks converted input line is: IncludehelP IS A GreaT PaltforM FoR GeekSRecommended 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
以線性時間復(fù)雜度對0、1和2的數(shù)組進(jìn)行排序
Finding subarray with given sum
查找給定總和的子數(shù)組
1[0]1 Pattern Count
1 [0] 1個樣式計數(shù)
Greedy Strategy to solve major algorithm problems
解決主要算法問題的貪婪策略
Job sequencing problem
工作排序問題
Exit Point in a Matrix
矩陣中的出口點
Generate Gray Code Sequences
生成格雷碼序列
Picking Numbers
領(lǐng)料號碼
Run-length encoding (find/print frequency of letters in a string)
游程編碼(字符串中字母的查找/打印頻率)
Count and Say sequence
計數(shù)并說出順序
Longest Common Prefix
最長的公共前綴
Count Substrings
計數(shù)子串
Number following the pattern
跟隨模式的數(shù)字
Next Permutation
下一個排列
翻譯自: https://www.includehelp.com/icp/capitalize-first-and-last-letter-of-each-word-in-a-line.aspx
如何設(shè)置單詞第一個字母大寫
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的如何设置单词第一个字母大写_大写一行中每个单词的第一个和最后一个字母的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何使用Tornado实现WebSock
- 下一篇: oracle中dbms_并发和由于DBM