日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何设置单词第一个字母大写_大写一行中每个单词的第一个和最后一个字母

發布時間:2025/3/11 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何设置单词第一个字母大写_大写一行中每个单词的第一个和最后一个字母 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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-1Find index of spaces in the lineIf index before spaces are not in hash tableInsert into hash tableIf index after spaces are not in hash tableInsert 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.

    在程序中包含哈希表有助于我們避免插入重復的索引。 否則,角落測試用例可能會失敗。

    .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 ++程序將一行中每個單詞的首字母和最后一個字母大寫 (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 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

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

    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的如何设置单词第一个字母大写_大写一行中每个单词的第一个和最后一个字母的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。