#include "stdafx.h"
#include "iostream"
#include "string"
usingnamespace std;
int main ()
{
string a;
char j[100];
int i, c, b;
cout <<"enter your name ";
getline(cin,a);
cout << " ur name is " << a << endl;
c=a.size();
for (b=0; b<=c; b++)
{
j[b]=a[b];
j[b]='\0';
}
system ("pause");
return 0;
}
how can u print every word typed on a different line?
#include <string>
#include <cstring>
#include <iostream>
#include <vector>
usingnamespace std;
int main() {
string name;
vector<string> tokens;
cout << "Enter a full name (Format: First Middle Last): ";
getline(cin, name);
// break line into words and save to a vector array
char *token = strtok((char *)name.c_str(), " ");
while (token) {
// save token to array
string s(token);
tokens.push_back(s);
// get next token
token = strtok(NULL, " ");
}
// Get first name and print
cout << "First name: ";
for (int i = 0; i < tokens.size() - 2; i++) {
cout << tokens[i] << " ";
}
cout << endl;
//Get middle name and print
cout << "Middle name: " << tokens[tokens.size() - 2] << endl;
//Get last name and print
token = strtok(NULL, " ");
cout << "Last name: " << tokens[tokens.size() - 1] << endl;
return 0;
}