The simplest, correct and most performative form would be this:
#include <stdio.h>
int main () {
char frase[200], palavra[200];
scanf(" %[^\n]s", frase);
int j = 0;
for (int i = 0; frase[i] != '\0'; i++) {
palavra[j] = frase[i];
j = palavra[j] == ' ' && frase[i + 1] != ' ' && frase[i + 1] != '\0' ? 0 : j + 1;
}
palavra[j] = '\0';
printf("%s", palavra);
}
But if the performance is more important there’s an even better way:
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference. (note the additional blanks at the beginning, middle and end).
I’m considering it may have a single huge word, so I kept the word size equal to the sentence.
When you use the strlen()
need to sweep the string all and then sweep again, and this is obviously a bad thing, so I’m stopping the loop when I find the terminator.
It copies character by character from frase
for a palavra
.
When there is a decision to change the value of the same variable in one way or another if a condition is true or false, it is usually best to use the conditional operator (I think it’s good to learn, because practically it’s just syntax change, the concept of condition you already understand).
The condition has to determine whether to reset the array of the word (j
) should be incremented. If not incremented, it should be zeroed, that is, forget everything you were holding and start again in the array of the word. But there are some tricks there.
If it’s a blank space, theoretically it should reset the counter because it starts a new word. But actually the word only starts if the next is a non-white character. So besides being a white the next should be a non-white.
Nor can it have the ending (null character \0
), since this would affect the index counter of palavra
and we need to know where he is to place the terminator.
If these conditions are not met, additional blanks give wrong result.
We need to put a terminator on array that receives the word, otherwise it potential will have no end and access memory that should not.
Then print the word according to the selected one. But it has a defect because if there is space at the end the space will be printed. You can fix this, but there was nothing in the question that indicated you need.
another failure is to find a tab or other blank character that separates words, or to find a separator symbol, such as punctuation (,
, .
, ?
, etc.), but it may be being used in a way that does not separate words, so everything becomes much more complicated to solve.
But if it is not important to check these cases of additional spaces, then a line solves:
#include <stdio.h>
#include <string.h>
int main () {
char frase[200];
scanf(" %[^\n]s", frase);
printf("%s\n", strrchr(frase, ' ') + 1);
}
The function strrchr()
just picks up the last block separated by some character.
You have some restraint of what you can use?
– Maniero
No, but the interesting thing would be something that does not go far beyond the knowledge of a beginner in programming.
– Erick