如何設置單詞第一個字母大寫_大寫一行中每個單詞的第一個和最后一個字母

如何設置單詞第一個字母大寫

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:

需要記住的其他幾件事:

  1. More than one occurrence of space in between two words.

    兩個單詞之間出現多個空格。

  2. There may be word of single letter like 'a', which need to be capitalized.

    可能有單個字母的單詞,例如'a' ,需要大寫。

  3. There may be word of two letters like 'me', where both the letters need to be capitalized.

    可能有兩個字母的單詞,例如“ me” ,其中兩個字母都必須大寫。

Algorithm:

算法:

  1. Create a hash table.

    創建一個哈希表。

  2. 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是輸入線的長度。

  3.     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
    
    
  4. Capitalize the indexes present in the hash table

    大寫哈希表中存在的索引

    line [index]-=32;

    行[index]-= 32;

  5. //ASCII value of lower case letter - ASCII value of corresponding upper case letter=32

    //小寫字母的ASCII值 -相應大寫字母的ASCII值 = 32

  6. 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

如何設置單詞第一個字母大寫

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542501.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542501.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542501.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

php如何編造簡歷,在簡歷里編造內容需要注意哪些問題?

在個人簡歷里編造內容可得有一定依據才行&#xff0c;總得為自己后期做個準備工作是不是&#xff1f;你編造的東西不只是給企業看一看而已&#xff0c;企業還會對這些內容作出進一步的判斷&#xff0c;并且可能就其對你進行提問&#xff0c;如果你答不出來而曝光自己是在欺騙企…

Java LinkedList公共對象pollLast()方法(帶示例)

LinkedList公共對象pollLast()方法 (LinkedList public Object pollLast() method) This method is available in package java.util.LinkedList.pollLast(). 軟件包java.util.LinkedList.pollLast()中提供了此方法。 This method is used to retrieves the last or ending ele…

python編寫學生成績排序_Python實現按學生年齡排序的實際問題詳解

前言 本文主要給大家了關于利用Python按學生年齡排序的相關內容&#xff0c;分享出來供大家參考學習&#xff0c;下面話不多說了&#xff0c;來一起看看詳細的介紹&#xff1a; 問題&#xff1a;定義一個Class&#xff1a;包含姓名name、性別gender、年齡age&#xff0c;需要按…

前方危險-讓很多“高逼格”高管深刻反思的文章

在很多的時候&#xff0c;現實會讓我們每個人迷惑&#xff0c;周邊的人和事可以讓人極度的膨脹&#xff0c;你可以想吃了迷藥一樣&#xff0c;分不清是現實還是虛幻。很久以前&#xff0c;在公司的一次會議上&#xff0c;某主管告訴我們說&#xff0c;“他一個同事&#xff0c;…

oracle實例的概念組成,oracle體系結構的兩個基本概念:數據庫和實例

您可能感興趣的話題&#xff1a;oracle核心提示&#xff1a;要了解oracle體系結構必須先了解兩個基本的概念: 數據庫和實例.要了解oracle體系結構必須先了解兩個基本的概念: 數據庫和實例.一: 數據庫數據庫(database)是一個數據集合.無論數據庫是采用關系結構還是面向對象結構,…

c#二維數據最大最小值_C#| 打印類型,各種數據類型的最大值和最小值

c#二維數據最大最小值In the below example – we are printing types, min value, max value of various data types in C#, like integer data types, floating point data types, Boolean data type, Reference types, Nullable types. 在下面的示例中-我們正在打印C&#x…

自定義taglib引入失敗_小程序拼團總失敗?看看微信官方和開發者們怎么說

閱讀時間&#xff1a;6m最懂小程序生態商業的自媒體可怕... 剛過國慶&#xff0c;南方還在短袖短裙&#xff0c;北方竟然都下雪了&#xff01;什么叫一天之內感受四季&#xff1f;曉程序觀察(yinghoo-tech)的小伙伴們算是深刻體驗了&#xff0c;穿著短袖上飛機&#xff0c;抵達…

微信公眾平臺開發5:翻譯功能

思路分析首先對用戶發送過來的消息進行判斷&#xff0c;判斷消息里是否含有“翻譯”關鍵字&#xff0c;如果有&#xff0c;則提取翻譯內容&#xff0c;然后調用網絡上開放的翻譯API 進行翻譯。我們用有道翻譯API&#xff1a;http://fanyi.youdao.com/openapi?pathdata-mode記下…

Linux之基礎I/O

目錄 一、C語言中的文件操作 二、系統文件操作I/O 三、文件描述符fd 1、文件描述符的引入 2、對fd的理解 3、文件描述符的分配規則 四、重定向 1、重定向的原理 2、重定向的系統調用dup2 五、Linux下一切皆文件 一、C語言中的文件操作 1、打開和關閉 在C語言的文…

moore和mealy_Mealy機和Moore機的比較研究 目錄

moore和mealyFinite automata may also have outputs corresponding to each input symbol. Such finite automata are known as finite automata with the output. 有限自動機還可以具有與每個輸入符號相對應的輸出。 這種有限自動機稱為輸出的有限自動機。 There are two fi…

oracle sys連接不上,oracle – 為什么我不能在SYS擁有的對象上創建觸發器?

在嘗試創建名為ghazal_current_bef_upd_row的觸發器時&#xff1a;create trigger ghazal_current_bef_upd_rowbefore update on ghazal_currentfor each rowwhen (new.Rating < old.Rating)begininsert into ghazal_current_audit(GhazalName,Old_Rating,New_Rating)values…

大一python編程題_請教python編程問題(作業就剩這幾道題了)

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓1. def cleanword(word):(用Python寫出程序&#xff0c;使程序可以通過下面的doctest)""">>> cleanword(what?)what>>> cleanword("now!")now>>> cleanword(?"word!,$…

Linux筆記1-5 --用戶

## 1 ## 用戶理解用戶就是系統使用者的身份在系統中用戶存儲為若干竄字符若干個系統配置文件用戶信息涉及到的系統配置文件&#xff1a;/etc/passwd ###用戶信息用戶&#xff1a;密碼&#xff1a;uid&#xff1a;gid&#xff1a;說明&#xff1a;家目錄&#xff1a;用戶使用…

python運維開發培訓_運維架構師-Python 自動化運維開發-014

運維架構師-Python 自動化運維開發-014九、標準數據類型1、為什么編程語言中要有類型類型有以下幾個重要角色&#xff1a;對機器而言&#xff0c;類型描述了內存中的電荷是怎么解釋的。對編譯器或者解釋器而言&#xff0c;類型可以協助確保上面那些電荷、字節在程序的運行中始終…

JavaScript | 演示函數中按值調用的示例

Here, we are designing a function named change() that has an argument and we are trying to change the value of the passed argument inside the function, but it will not effect to the main/actual argument that is passed as the argument while calling. 在這里&…

機器視覺支架制作(帶效果測試)

圖像處理系統中&#xff0c;鏡頭、光源的選配&#xff0c;對于最后能否產生穩定的識別效果至關重要。而搭載鏡頭、光源的是支架。機器視覺的支架一般都是根據項目的具體需要進行配置的&#xff0c;搜索淘寶能夠得到一些商品。 這些支架形狀不一&#xff0c;價格在數百元到千元之…

c語言中將整數轉換成字符串_在C語言中將ASCII字符串(char [])轉換為十六進制字符串(char [])...

c語言中將整數轉換成字符串Given an ASCII string (char[]) and we have to convert it into Hexadecimal string (char[]) in C. 給定一個ASCII字符串(char [])&#xff0c;我們必須在C中將其轉換為十六進制字符串(char [])。 Logic: 邏輯&#xff1a; To convert an ASCII …

redis rdb aof區別_理解Redis的持久化機制:RDB和AOF

什么是Redis持久化?Redis作為一個鍵值對內存數據庫(NoSQL)&#xff0c;數據都存儲在內存當中&#xff0c;在處理客戶端請求時&#xff0c;所有操作都在內存當中進行&#xff0c;如下所示&#xff1a;這樣做有什么問題呢&#xff1f;注 意文末有&#xff1a;3625頁互聯網大廠面…

python--批量下載豆瓣圖片

溜達豆瓣的時候&#xff0c;發現一些圖片&#xff0c;懶得一個一個扒&#xff0c;之前寫過c#和python版本的圖片下載&#xff0c;因此拿之前的Python代碼來改了改&#xff0c;折騰出一個豆瓣版本&#xff0c;方便各位使用 # -*- coding:utf8 -*- import urllib2, urllib, socke…

linux touch權限不夠,Linux下的Access、Modify、Change , touch的使用以及權限問題

每個文件在linux下面都會記錄許多的時間參數&#xff0c;其實是有三個主要的變動時間&#xff0c;那么&#xff0c;這三個時間的意義又是什么&#xff1f;下面我們來介紹&#xff1a;* Modify time(mtime)當該文件的“內容數據”更改時&#xff0c;就會更新這個時間。內容數據指…