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

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

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.mzph.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;就会更新这个时间。内容数据指…