Computer Science, asked by aljuveriyafatima, 10 months ago

Write a program to accept a string and print the initials of name such that the surname
appears first followed by the initials.
Input: Vallabh Bhai Patel
Output: Patel VB

PLZ ANYONE SOLVE IT​

Answers

Answered by shailjasinha523
1

Answer:

C++ program to print the initials

// of a name with the surname

#include <bits/stdc++.h>

using namespace std;

void printInitials(string str)

{

int len = str.length();

// to remove any leading or trailing spaces

str.erase(0, str.find_first_not_of(' '));

str.erase(str.find_last_not_of(' ') + 1);

// to store extracted words

string t = "";

for (int i = 0; i < len; i++)

{

char ch = str[i];

if (ch != ' ')

// forming the word

t = t + ch;

// when space is encountered

// it means the name is completed

// and thereby extracted

else

{

// printing the first letter

// of the name in capital letters

cout << (char)toupper(t[0]) << ". ";

t = "";

}

}

string temp = "";

// for the surname, we have to print the entire

// surname and not just the initial

// string "t" has the surname now

for (int j = 0; j < t.length(); j++)

{

// first letter of surname in capital letter

if (j == 0) temp = temp + (char)toupper(t[0]);

// rest of the letters in small

else

temp = temp + (char)tolower(t[j]);

}

// printing surname

cout << temp << endl;

}

// Driver Code

int main()

{

string str = "ishita bhuiya";

printInitials(str);

}

hope it helps you

thanku

Answered by aslisona36
2

Answer:

by using documentation comment

if any mistake so please ignore I'm just learning computers I'm in class 10th

Explanation:

// C++ program to print the initials

// of a name with the surname

#include <bits/stdc++.h>

using namespace std;

void printInitials(string str)

{

int len = str.length();

// to remove any leading or trailing spaces

str.erase(0, str.find_first_not_of(' '));

str.erase(str.find_last_not_of(' ') + 1);

// to store extracted words

string t = "";

for (int i = 0; i < len; i++)

{

char ch = str[i];

if (ch != ' ')

// forming the word

t = t + ch;

// when space is encountered

// it means the name is completed

// and thereby extracted

else

{

// printing the first letter

// of the name in capital letters

cout << (char)toupper(t[0]) << ". ";

t = "";

}

}

string temp = "";

// for the surname, we have to print the entire

// surname and not just the initial

// string "t" has the surname now

for (int j = 0; j < t.length(); j++)

{

// first letter of surname in capital letter

if (j == 0) temp = temp + (char)toupper(t[0]);

// rest of the letters in small

else

temp = temp + (char)tolower(t[j]);

}

// printing surname

cout << temp << endl;

}

// Driver Code

int main()

{

string str = "ishita bhuiya";

printInitials(str);

}

Similar questions