除姓氏外,我如何取名字中所有部分的首字母?
How do i take the initials of all the parts of one's name except the last name?
嗨,我正在尝试编写一个C ++程序,用户将在其中输入一个名称,例如:Tahmid Alam Khan 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();
}
我想解决这个问题的最简单方法是标记您的字符串,打印其中的第一个字符,除了最后一个字符,您打印其全尺寸。
要标记化,您可以执行以下操作:
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;
}
我希望这对:)有所帮助它可能是一个更简单的版本(最初我是用 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]=' '; //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]!=' ') //not the space after surname
{
printf("%c.",str[i+1]); //prints just the initial
}
else
for(k=i+1;str[k]!=' ';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();
}
- 函数何时会在c++中包含stack_Unwind_Resume调用
- Python中的for循环与C++有何不同
- 在C++中释放内存期间,迭代器与指针有何不同
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- 类中的 C++ int 被设置为值,似乎不知从何而来
- -fvisibility-inline-hidden 与 gcc 中的 -fvisibility=hidden 有何不同
- 收益率和回报有何不同?
- 覆盖私有功能,它与受保护功能有何不同?
- 擦除删除成语的性能增益从何而来
- 无论代码长度如何,以下代码的内存要求有何不同?
- "virtual"对C++析构函数有何影响?
- 如果我对"while"块发表评论,为什么程序会死机?其中的"yield"线有何影响?
- 系统时间从何而来?
- stl::unordered_map 和 stl::vector 的销毁有何不同
- C++ 友元函数在内存位置上有何不同?
- 为什么我可以在不链接任何额外库的情况下包含 sys/*.h
- C 是否具有接口类概念,如果它在那里,那么它与Java接口类别有何不同
- std::u8string与std::string有何不同?
- 功能和变量从何而来
- 两种类型转换有何不同?