GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

WAP to print initials of a given first name,middle name,and last name ?

asked Mar 19, 2017 by PRITAM VIVEK

1 Answer

#include<bits/stdc++.h>
using namespace std;

int main(){
	string str;
	getline(cin,str);
	int flag=0;
	for(int i=0; i<str.length(); i++){
		if(str[i]==32)
			flag=1;
	}
	int lastspace=0;
	if(flag!=0){
		for(int i=0; i<str.length(); i++){
			if(str[i]==32)
				lastspace=i;
		}
		int temp=1;
		for(int i=0; i<lastspace; i++){
			if(temp==1){
				cout<<str[i]<<".";
				temp=0;
			}
			else if(str[i]==32){
				temp=1;
			}
		}
		for(int i=lastspace; i<str.length(); i++)
			cout<<str[i];
	}
	else
		cout<<str;
	
//	cout<<str;
	return 0;
}

 

answered Mar 30, 2017 by Ratnesh Singh
...