Q:

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

0

Capitalize first and last letter of each word in a line

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.
  3. There may be word of two letters like 'me', where both the letters need to be capitalized.

Algorithm:

  1. Create a hash table.
  2. Insert index of first letter, i.e., 0 & the index of last letter, i.e., length-1length be the length of input line.
  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;
  5. //ASCII value of lower case letter - ASCII value of corresponding upper case letter=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++ 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 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now