Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- char *copyLastWord(char *sz);
- int main()
- {
- char sz[32];
- cout << "Your full name: ";
- fflush(stdin); cin.getline(sz, 32);
- cout << endl;
- cout << "This is the full name: " << sz << endl;
- char *word;
- word = copyLastWord(sz);
- cout << "This is the last word of your name: " << word << endl;
- system("pause");
- return 0;
- }
- // Copy the last word of the string
- char *copyLastWord(char *sz)
- {
- char *word = new char;
- int i, j;
- for (i = 0, j = 0; sz[i] != '\0'; i++)
- {
- if (sz[i] != ' ')
- {
- word[j] = sz[i];
- word[j + 1] = '\0';
- j++;
- }
- else // if (sz[i] == ' ')
- j = 0;
- }
- return word;
- }
- char *cutLastWord(char *sz)
- {
- char *word = new char;
- strcpy(word, sz);
- strrev(word);
- int i = 0;
- while (word[i] != ' ')
- i++;
- strncpy(word, word, i);
- word[i] = '\0';
- strrev(word);
- return word;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement