The simplest, most correct and most performative 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] != '#include <stdio.h>
#include <string.h>
int main () {
char frase[200];
scanf(" %[^\n]s", frase);
printf("%s\n", strrchr(frase, ' ') + 1);
}
'; i++) {
palavra[j] = frase[i];
j = palavra[j] == ' ' && frase[i + 1] != ' ' && frase[i + 1] != '#include <stdio.h>
int main () {
char frase[200], palavra[200];
scanf(" %[^\n]s", frase);
int j = 0;
for (int i = 0; frase[i] != '#include <stdio.h>
#include <string.h>
int main () {
char frase[200];
scanf(" %[^\n]s", frase);
printf("%s\n", strrchr(frase, ' ') + 1);
}
'; i++) {
palavra[j] = frase[i];
j = palavra[j] == ' ' && frase[i + 1] != ' ' && frase[i + 1] != '%pre%' ? 0 : j + 1;
}
palavra[j] = '%pre%';
printf("%s", palavra);
}
' ? 0 : j + 1;
}
palavra[j] = '%pre%';
printf("%s", palavra);
}
But if performance = more important has an even better way:
See running on ideone . And in Coding Ground . Also I placed GitHub for future reference . (note the additional blanks at the beginning, middle, and end).
I'm considering that you can have one huge word, so I kept the word size equal to the one in the sentence.
When using strlen()
you need to scan the entire string and then scan again, and this is obviously a bad thing, so I'm stopping the loop when I find the terminator.
You are copying character by character from frase
to palavra
.
When there is a decision to change the value of the same variable in one form or another if a condition is true or false, it is usually better to use the conditional operator (I think it is a good learning, because it is practically just a change of syntax, the concept of condition you already understand).
The condition has to determine whether to clear the array counter of the word ( j
) should be incremented. If it is not incremented, it must be zeroed, ie forget everything you were saving and start again in the array of the word. But there's some catch-ups there.
If it is a blank space, theoretically it should reset the counter because a new word begins. But in fact the word only begins if the next one is a nonwhite character. So besides being a white the next should be a non-white.
And also can not have it terminated (null character palavra
), since this would affect the index counter of ,
and we need to know where it is to put the terminator.
If these conditions are not observed, additional blanks give the wrong result.
We need to put a terminator in the array that receives the word, otherwise its potential will have no end and access memory it should not.
Then it prints the word according to the selected one. But it has a defect because if it has space at the end the space will be printed. You can set this up, but you had nothing in the question to indicate that you need it.
Another failure is if you find a tab or other blank character that separates words, or if you find a separator symbol, such as punctuation ( .
, ?
, strrchr()
, etc.), but it may be being used so as not to separate words, then everything gets much more complicated to solve.
But if it is not important to check these cases for additional spaces, then one line resolves:
%pre%
The function %code% just takes the last block separated by some character.