FOR FREE YEAR SOLVED

Name Abbreviation

Back to Programming

Description

In this program, a string is taken as an input. The input string is the name of a person and the name should contain four parts like Mr. Pinaki Ranjan Sinha. Now, the program is written to print the name in short form. So, the aforesaid name will be printed as Mr. P. R. Sinha.

For this type of name, there are 3 spaces. The substring before the first space and the substring after the last space is fully copied and for the substrings after 1st and 2nd space, only the first character after space will be copied. Hence, we will get the output string.

Algorithm

INPUT: A name with four parts
OUTPUT: The short form of the given name
PROCESS:
Step 1: [Taking the input]
	Read s [the name as aforesaid format]
Step 2: [Making the short form of the name]
	Set sp<-0
	Set in<-0
	Set n<- length of the string ‘name’
	[abbreviating the name]
	For i=0 to n-1 repeat
		[if the character is space]
		If name[i]=' ' then
			Set sp<-sp+1
		[End of ‘if’]
		[the first and last part of the name will be copied fully]
		If sp=0 or sp=3 then
			Set ab[in]<-name[i]
			Set in<-in+1
		[End of ‘if’]
		[for the middle part of the name only first character will be taken]
		If name[i]=' ' and sp<3 then
			Set ab[in]<-' '
			Set in<-in+1
			Set ab[in]<-name[i+1]
			Set in<-in+1
			Set ab[in]<-'.'
			Set in<-in+1
			Set i<-i+1
		[End of ‘if’]
	[End of ‘for’ loop]
	[printing the abbreviated name]
	Print "The name after abbreviation: "
	Print ‘ab’
[End of abbreviation]
Step 3: Stop.

Code

Time Complexity:

The time complexity of this program is O(n) where ‘n’ is the number of characters of the input string.

 

Space Complexity:

The space complexity of this program is O(n) where ‘n’ is the number of characters of the input string.