NITIKA - editorial

PROBLEM LINK:

Practice
Contest

Author: Abhinav Jain
Primary Tester: Misha Chorniy
Editorialist: Hussain Kara Fallah

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

Given names of people. Each name may consist of at least one and at most three parts. You are asked to show the name with replacing the first two parts (if they exist) with the first letter of each (abbreviation) and followed by the last part (last name). Abbreviations should be upper case letters. Only the first letter of the last name should be in uppercase.

EXPLANATION:

This problem is straight forward implementation (string manipulation). Each name will be given on a separate line, so we should read each line completely (one by one). We have to find the parts of each name and separate them from each other. Since each two consecutive parts are separated by a space,we should be looking for spaces in each line. The first space (if it exists) separates between the 1st and the 2nd part, the second space (if it exists) separates between the 2nd and the 3rd part. Finding spaces on each line would make us able to break our full name into parts (This can be done manually by a loop).

My solution uses stringstream (C++ class) which is very useful for parsing input and solves this problem easily. A brief explanation can be found here :stringstream

After breaking the name into parts we should capitalize the first letter of each part. We should output only the first letter of each part capitalized (except the last part). As for the last part, we must set its first letter to uppercase, and the rest of its letters to lowercase. After that, we can print it.

AUTHOR’S AND TESTER’S SOLUTIONS:

AUTHOR’s solution: Will be found here
TESTER’s solution: Will be found here
EDITORIALIST’s solution: Will be found here

2 Likes
<?php $fp = fopen("php://stdin","r"); $value1 = fgets($fp); $members = array(); while (!feof($fp)) { $members[] = fgets($fp); } for($i = 0;$i <= $value1-1;$i++) { $value = explode(" ",$members[$i]); $count = count($value); if($count < 2) { echo ucwords($value[$count-1]); } else { for($i = 0;$i <= $count -1;$i++) { if($i == $count-1 ) echo '.'.ucwords($value[$i]); else echo ucwords($value[$i][0]); } } }

if($i == $count-1 )
echo ‘.’.ucwords($value[$i]);
else
echo ucwords($value[$i][0]);
}
}
}

if($i == $count-1 )
echo ‘.’.ucwords($value[$i]);
else
echo ucwords($value[$i][0]);
}
}
}

var myName= [‘s’,‘S’,‘o’,‘O’,‘u’,‘U’,‘b’,‘B’,‘h’,‘H’,‘a’,‘A’,‘g’,‘G’,‘y’,‘Y’,‘a’,‘A’
‘Shekhar’,‘shekhar’]
var listItem = myName[1] ,[18]
console.log (listItem)

#include<stdio.h>
#include<string.h>
#include<ctype.h>

int main()
{

char str[100001];
int temp;
int i;
int t;
scanf("%d\n",&t);
while(t-- ){
gets(str);
temp=0;
for(i=0;i<strlen(str);i++){
if(str[i]==’ '){
printf("%c. ",toupper(str[temp]));
temp=i+1;}
}

printf("%c",toupper(str[temp]));
for(i=temp+1;i<strlen(str);i++)
{
printf("%c",tolower(str[i]));
}

printf("\n");
}

return 0;
}

#include<stdio.h>
#include<string.h>
#include<ctype.h>

int main()
{

char str[100001];
int temp;
int i;
int t;
scanf("%d\n",&t);
while(t-- ){
    gets(str);
    temp=0;
    for(i=0;i<strlen(str);i++){
        if(str[i]==' '){
            printf("%c. ",toupper(str[temp]));
            temp=i+1;}
    }

    printf("%c",toupper(str[temp]));
    for(i=temp+1;i<strlen(str);i++)
    {
        printf("%c",tolower(str[i]));
    }

printf("\n");
}

return 0;

}

solved. :smiley:

what is wrong with this??

#include <bits/stdc++.h>

using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);

int t;
cin>>t;
cin.ignore(1);
while(t–)
{
cin >> ws;
string s;
getline(cin,s);
int i,j,cnt=0,cnt1=0,num=0;
vector v;
string s1;
for(i=0;i<s.length();++i)
{
if(s[i]!=’ ‘)
{
for(j=i;j<s.length();++j)
{
if(s[j]==’ ')
break;

                  else
                    s1.push_back(s[j]);
              }
              
              v.push_back(s1);
              s1.clear();
              i=j;
          }
   }
   
   if((int)v.size()==1)
     {
         if(v[0][0]>='a' && v[0][0]<='z')
             v[0][0]-=32;
             
        cout<<v[0]<<endl;         
     }
     
    else
     {
         for(i=0;i<v.size()-1;++i)
         {
             if(v[i][0]>='a' && v[i][0]<='z')
                {
                    cout<<(char)(v[i][0]-32)<<". ";
                }
                
            else
              cout<<v[i][0]<<". ";
         }
         
         if(v[v.size()-1][0]>='a' && v[v.size()-1][0]<='z')
             v[v.size()-1][0]-=32;
         
         for(i=1;i<v[v.size()-1].length();++i)
         {
             if(v[v.size()-1][i]>='A' && v[v.size()-1][i]<='Z')
                v[v.size()-1][i]+=32;
         }
         
         cout<<v[v.size()-1]<<endl;
     }

}

 return 0;

}

/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
String s = “”;
sc.nextLine();

	while(t-- > 0){
	    s = sc.nextLine();    
	    String ans = "";
	    
	    String[] splitted = s.split(" ");
	    System.out.println(splitted.length);
	    
	    if(splitted.length == 0)
	        ans = "";
	    else if(splitted.length == 1){
	        ans = s.substring(0,1).toUpperCase() + s.substring(1).toLowerCase(); 
	    }
	    else{
	        for(int i = 0; i < (splitted.length) - 1 ; i++){
	            ans = ans + splitted[i].substring(0,1).toUpperCase() + ". ";
	        }
	        ans += splitted[(splitted.length)-1].substring(0,1).toUpperCase() + splitted[(splitted.length)-1].substring(1).toLowerCase();
	    }
	    System.out.println(ans);
	}
}

}

can you please tell me why it give me wrong answer at submission?

PYTHON CODE

for _ in range(int(input())):
list1 = list(input().split())
if(len(list1)==1):
list1[0] = list1[0].title()
print(list1[0])
elif(len(list1)>1):
list1[-1] = list1[-1].title()
for k in range(0,len(list1)-1):
list1[k]= list1[k][0].upper() + ‘.’
print(*list1)

‘’‘GOOD DAY ! ‘’’
:wink:

I would request you to go through my code once , it will give you a better understanding on the logical aspect !!

:slight_smile:

thank you for your suggestion. but my code is right just one mistake i made was printing length of splitted string. :slightly_smiling_face: