String Coding Problems

Arrays Coding Problems

Sorting Coding Problems

Searching Coding Problems

Coding Algorithms

Tree Coding Problems

Stack Coding Problems

Linked list Coding Problems

Graph Coding Problems

Greedy Algorithms Coding Problems

Dynamic Programming Coding Problems

Matrix Coding Problems

Recursion Coding Problems

Number Theory Coding Problems

Backtracking Coding Problems

Heap Coding Problems

Brute Force Coding Problems

Implementation Coding Problems

Google Contests

Competitive Programming Coding


Capitalize first and last letter of each word in a line

In this article, we are going to learn how to capitalize first and last letter of each word in an input line?
Submitted by Radib Kar, on November 27, 2018

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.


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.


  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.
  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
				table.insert(j-1); //if not insert index
			//check index already present in hash table or not
				table.insert(j+1); //if not insert index
	for(auto it=table.begin();it!=table.end();it++)
	printf("converted input line is: ");
	for(int j=0;j<i;j++)

int main(){
	//store the input line
	char arr[100];
	char c;
	int i=0;

	printf("input the line.....\n");
	return 0;


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 

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.