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

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

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

推荐的帖子

翻译自: https://www.includehelp.com/icp/capitalize-first-and-last-letter-of-each-word-in-a-line.aspx

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值