2014-12-05 40 views
0

嗨,我想寫一個C++程序,用戶將輸入一個名稱可以說,例如:塔米阿拉姆汗Rifat和計算機將打印名稱的格式化版本,在這種情況下,將是:T. A. K. 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(); 
} 
+1

我們可以看到一些輸出嗎? – Resource 2014-12-05 14:00:37

+1

請修復您的格式。只需簡單地縮進所有代碼,而不是將反引號放在任何地方。 – leemes 2014-12-05 14:02:24

回答

0

你的循環更改爲以下: -

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 << "."; 
    } 
} 
+0

不工作的人:( – nz881 2014-12-05 16:18:43

0

嘗試拉維的回答使你的代碼的工作,但我想指出的是,有更直觀的方式來設定此這將使維護並在未來更容易合作(總是一個好的做法)。

您可以使用explode()實現(或C的strtok())將名稱字符串拆分爲多個部分。然後,只需使用每件作品的第一個字符,無視姓氏。

0

我認爲你的問題已經得到解答。但是將來你可以考慮將你的程序分解爲更簡單的任務,這使得閱讀起來更容易。結合描述性變量和函數名稱,它可以使程序更容易理解,因此稍後修改或修復。 免責聲明 - 我是一個初學者的業餘程序員,這僅僅是想法:

#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(); 
} 
0

我想解決這個來標記你的字符串,從它打印的第一個字符,除了從上,其中最簡單的方法你打印它的全尺寸。

來標記,你可以做這樣的事情:

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; 
} 
1

我希望這有助於:) 這可能是一個簡單的版本(最初我是用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 
       } 
      } 
     } 



    }