+ 1
C program to print last word in a sentence
How can I print last word in a sentence?
13 ответов
+ 6
Try to use array, then find the last element and print it.
+ 3
Personally, I'd iterate over the sentence until I find the terminating null character and reset a last_word variable every time I find a space.
Something like this (pseudo code):
while char != '\0'
char = sentence[i]
last_word += char
if char == ' '
last_word = ''
If you need help with a specific code, please upload the code to the Code Playground and post a link here
https://www.sololearn.com/Discuss/1316935/welcome-to-sololearn-forum/
+ 3
this way is much simpler without loops.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "this is a sentence.";
char *word = strrchr(str, ' ') + 1;
printf("%s\n", word);
return 0;
}
+ 2
I think you should show your try, on this, and if it's the logic behind it you need someone to explain to you, just ask
+ 2
I don't know why you guys keep on giving the solution out instead of a hint for him to think like a programmer and get problems solved, am sure nobody will have a food on the table and say that he want to re-cook another because they do not make that themselves
+ 2
Reverse loop from end of string to previous word 😎
0
Pointers aren't used yet so I cannot use them.
0
Figured it out, thanks for help.
0
Thanks
0
Is it in c program from which we write a pseudocode?