[英]How to get all initials of name stored in an array using 2 for loops?
[英]How do i take the initials of all the parts of one's name except the last name?
嗨,我正在嘗試編寫一個c ++程序,用戶將輸入一個名稱,例如:Tahmid Alam Khan Rifat,計算機將打印該名稱的格式版本,在這種情況下,它將是:TAK Rifat先生。 我已包含以下代碼。 您將能夠看到我已經接近了,但仍然不是我想要的。 請幫忙。
#include<iostream>
#include<string>
using namespace std;
class myclass{
private:
string name,temp;
string p;
int i,j,sp;
public:
void work(){
cout << "Enter the name of the male student: ";
getline(cin,name);
cout << endl;
cout << "The original name is: ";
cout << name;
cout << endl << endl;
cout << "The formatted name is: " << "Mr." << name[0] << ".";
for(i=0;i<name.size();i++){
if(name[i]==' '){
sp=i;
for(j=sp+1;j<=sp+1;j++){
temp=name[j];
cout << temp << ".";
}
}
}
for(i=sp+2;i<name.size();i++){
cout << name[i];
}
cout << endl;
}
};
int main(){
myclass c;
c.work();
}
我希望這會有所幫助:)它可能是一個簡單的版本(本來我用C編寫的,但是由於邏輯保持不變,您可以輕松地將其轉換為C ++)。 我已經接受了該名稱,然后在字符串的開頭插入了一個空格,在NULL字符('\\ 0')之前的末尾又插入了一個空格。
程序檢查空格。 遇到一個空格時,它會檢查字符串中下一個空格。 現在,該空間的出現有助於我們確定下一個動作應該是什么的重要決定因素。
看,如果在此后續空格之后有一個空字符,那么我們可以得出結論,該后續空格是我們在字符串末尾插入的空格。 也就是說,該空間位於主要空間之后,該空間位於姓氏之前。 答對了! 您將從姓氏開始的地方獲得數組的精確索引! :d
看起來很長,但確實很簡單。 祝好運!
#include<stdio.h>
#include<string.h>
void main()
{
char str[100]; /*you could also allocate dynamically as per your convenience*/
int i,j,k;
printf("Enter the full name: ");
gets(str);
int l=strlen(str);
for(i=l;i>=0;i--)
{
str[i+1]=str[i]; //shifting elements to make room for the space
}
str[0]=' '; //inserting space in the beginning
str[l+1]=' '; str[l+2]='\0'; //inserting space at the end
printf("The abbreviated form is:\n");
for(i=0;i<l+1;i++) //main loop for checking
{
if(str[i]==' ') //first space checker
{
for(j=i+1; str[j]!=' ';j++) //running loop till subsequent space
{
}
if(str[j+1]!='\0') //not the space after surname
{
printf("%c.",str[i+1]); //prints just the initial
}
else
for(k=i+1;str[k]!='\0';k++) //space after surname
{
printf("%c", str[k]); //prints the entire surname
}
}
}
}
將循環更改為以下內容:
for(i=0;i<name.size();i++)
{
if(name[i]==' ')
{
initial = i + 1; //initial is of type int.
temp = name[initial]; //temp is char.
cout << temp << ".";
}
}
嘗試用ravi的答案使您的代碼正常工作,但我想指出,有更直觀的方法對此進行編程,這將使將來的維護和協作更加容易(始終是一種好習慣)。
您可以使用explode()實現(或C的strtok())將名稱字符串拆分為多個部分。 然后,只需使用每個作品的第一個字符,而不必考慮姓氏。
我認為您的問題已經得到解答。 但是在將來,您可以考慮將程序分成更簡單的任務,這使事情更容易閱讀。 結合描述性的變量和函數名稱,它可以使程序更易於理解,因此可以在以后進行修改或修復。 免責聲明-我是一名初學者業余程序員,這僅是一些想法:
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
// I got this function from StackOverflow somewhere, splits a string into
// vector of desired type:
template<typename T>
std::vector<T> LineSplit(const std::string& line) {
std::istringstream is(line);
return std::vector<T>(std::istream_iterator<T>(is), std::istream_iterator<T>());
}
class Names {
private:
std::vector<std::string> full_name_;
void TakeInput() {
std::cout << "Enter the name of the male student: " << std::endl;
std::string input;
getline(std::cin,input);
full_name_ = LineSplit<std::string>(input);
}
void DisplayInitialsOfFirstNames() const {
std::cout << "Mr. ";
for (std::size_t i = 0; i < full_name_.size()-1; ++i) {
std::cout << full_name_[i][0] << ". ";
}
};
void DisplayLastName() const {
std::cout << full_name_.back() << std::endl;
}
public:
void work() {
TakeInput();
DisplayInitialsOfFirstNames();
DisplayLastName();
};
};
int main(){
Names n;
n.work();
}
我猜想解決此問題的最簡單方法是標記字符串,從中打印第一個字符,但從最后一個開始打印完整字符。
要標記化,您可以執行以下操作:
std::vector<std::string> tokenize(std::istringstream &str)
{
std::vector<std::string> tokens;
while ( !str.eof() ) {
std::string tmp;
str >> tmp;
tokens.push_back(tmp);
}
return tokens;
}
現在您可以輕松地遍歷令牌:
int main()
{
std::string name;
cout << "Enter the name of the male student: ";
getline(cin,name);
cout << endl;
cout << "The original name is: ";
cout << name;
cout << endl << endl;
std::istringstream str(name);
std::vector<std::string> tokens = tokenize(str);
for ( int i = 0 ; i < tokens.size() - 1; ++i)
std::cout << tokens[i][0] << ". ";
cout << tokens[tokens.size() - 1] << endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.