如何设置单词第一个字母大写

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.

在程序中包含哈希表有助于我们避免插入重复的索引。 否则,角落测试用例可能会失败。

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

如何设置单词第一个字母大写

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

  1. python单词首字母大写_在Python中将每个单词的首字母大写

    python单词首字母大写 Here, we are implementing a python program to capitalizes the first letter of each wor ...

  2. python猜单词游戏心得_【Python】猜单词游戏

    #猜单词游戏 import random #创建单词序列 WORDS = ['python', 'shampoo', 'war', 'despair', 'distach', 'ultimate', ...

  3. bootice添加黑苹果引导_黑苹果设置从硬盘引导_在PE中写入UEFI引导启动项

    摘要 将黑苹果设置为硬盘引导,从而摆脱移动硬盘或U盘引导,网上一查有很多方法,比较常用的就是在 Windows中用Easy UEFI工具完成,但这要求你的Windows必须是基于UEFI启动,如果不是 ...

  4. 计算机磁盘为uefi引导,黑苹果设置从硬盘引导_在PE中写入UEFI引导启动项

    当费劲九牛二虎之力将黑苹果安装好以后,每次启动Mac OS的时候,却需要插入U盘来引导,实在很麻烦和别扭,因此我们需要将黑苹果设置从硬盘启动,以摆脱对U盘引导的依赖. 在<黑苹果E3-1231- ...

  5. python编程试题单词倒排_Python:将句子中的单词全部倒排过来,但单词的字母顺序不变...

    早上看到好友未央的一篇博文<一道google的测试工程师笔试题>,内容如下: 这是去年面试google测试工程师的一道题,题目如下: 设计一个函数,使用任意语言,完成以下功能: 一个句子, ...

  6. Java怎么查找字符串大写_在Java中,如何检查字符串是否包含子字符串(忽略大小写)?...

    本问题已经有最佳答案,请猛点这里访问. 我有两个String s,str1和str2. 如何检查str1是否包含在str1中,忽略大小写? indexOf和contains都是逐个字符的,所以如果你需 ...

  7. node 大写_大写Node.js模块

    node 大写 Today, let's see a third party module that helps us in working with upper-case letters witho ...

  8. python随机列表文本_在python中从单词列表返回随机单词

    >>> import random >>> random.choice(list(open('/etc/dictionaries-common/words'))) ...

  9. 英语背单词有用吗_对于大学生英语背单词软件哪个好可以用_最好的背单词

    对于大学生英语背单词软件哪个好可以用_最好的背单词 内容预览 大学背单词软件abundance.mp4 大学背单词软件abuse.mp4 大学背单词软件bum.mp4 大学背单词软件burden.mp ...

最新文章

  1. 使用php读写mysql数据库并显示到网页上
  2. SQLSERVER中如何忽略索引提示
  3. 为什么使用了索引,查询还是慢?
  4. Android官方开发文档Training系列课程中文版:分享文件之配置文件共享
  5. 老兵的十年职场之路(一)
  6. 简述java的异常处理机制_简述java异常处理机制
  7. Linux下的sniffer工具--Tcpdump的安装和使用
  8. Python4班平均成绩统计_回首过去,展望未来 | 欢迎大家来到E班第一次主题班会!...
  9. 2021下半年最新编程培训机构排名出炉!
  10. 超实用的大学网课答案搜题软件及公众号有哪些?
  11. 腾讯qq群推广“一键加群”的一个细节
  12. base64在线解码(等号或者双等号结尾的数据的解码)
  13. 【12】Kotlin函数泛型协程
  14. webpack整合bable
  15. 瑞芯微rk2818(android系统2.1),从Android 2.1开始 瑞芯微RK2818要火
  16. 最适合python程序员用的笔记本-JupyterLab:程序员的笔记本
  17. php服务器文件直链,比较简单的百度网盘文件直链PHP代码
  18. VL31N创建内向交货函数GN_DELIVERY_CREATE及增强字段
  19. 转 ROS:解决Error:cannot launch node of type [map_server/map_server]: can't locate node [map_server]
  20. python中心性评价_复杂网络中边的中心性(Edge Centrality)

热门文章

  1. distinct返回null报错_C#之集合常用扩展方法与Linq
  2. 三星w系列vip服务器,高端人士候机专属特权 三星W2017一张行走的VIP卡
  3. php 网络图片 转本地,PHP将Base64图片转换为本地图片并保存
  4. git merge 回退_Git 基础学习总结2(学不会你锤我)
  5. 路由与交换技术(铺垫内容)
  6. 如何把WAV格式音频转换为MP3格式
  7. IBM研究院计画5年改变人类生活创新预测
  8. 【原创】利用腾讯和百度的AI接口识别验证码
  9. Linux命令之find命令中的-mtime参数
  10. drbd(三):drbd的状态说明