+ 1

C program to print last word in a sentence

How can I print last word in a sentence?

4th May 2019, 11:04 AM
muds huds
muds huds - avatar
13 Réponses
+ 6
Try to use array, then find the last element and print it.
4th May 2019, 11:26 AM
voja
voja - avatar
+ 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/
4th May 2019, 11:28 AM
Anna
Anna - avatar
+ 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; }
4th May 2019, 12:40 PM
Shen Bapiro
Shen Bapiro - avatar
+ 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
4th May 2019, 12:19 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 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
4th May 2019, 4:53 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 2
Reverse loop from end of string to previous word 😎
5th May 2019, 5:05 PM
Sanjay Kamath
Sanjay Kamath - avatar
0
Pointers aren't used yet so I cannot use them.
4th May 2019, 1:10 PM
muds huds
muds huds - avatar
0
Figured it out, thanks for help.
5th May 2019, 9:22 PM
muds huds
muds huds - avatar
0
Thanks
6th May 2019, 9:46 AM
Musa Osman
Musa Osman - avatar
0
Is it in c program from which we write a pseudocode?
6th May 2019, 10:02 AM
Beatrice Ngari
Beatrice Ngari - avatar