Reverse The Words In A String | C Programming Example

  Рет қаралды 27,080

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер: 18
@durkaperiasamy2405
@durkaperiasamy2405 10 ай бұрын
Im from India i found ur video's very late...boz semester (final)exams are near but after final i promise i will complete ur c programming playlist....❤🎉😊
@Dhana2100
@Dhana2100 Ай бұрын
Did You?
@ManikantaSaiTejaMathi
@ManikantaSaiTejaMathi Ай бұрын
Nice set of programs
@warplanner8852
@warplanner8852 8 ай бұрын
Use strtok() to strcpy() words into an array of strings. Then traverse the array in reverse order. Advantage: easier to implement. (Specious advantage). Disadvantage: cannot process an infinite string.
@madhubabu3482
@madhubabu3482 Жыл бұрын
The occurences or frequency of words in a string. Please do it.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Is this video what you're looking for Madhu? :-) Video: kzbin.info/www/bejne/kKrHl2ymZruCfbc
@mohamedelamine1640
@mohamedelamine1640 2 жыл бұрын
hey sir ! sorry if it is dumb question but i'm new to programming , so when you said we gonna check whether we reached the end of the string or not , what if the string doesn't end with a space or dot , so the last word in string will not be detected per eg: "good morning sir" so the word "sir" will not be detected unless the string is like that "good morning sir." or that "good morning sir "
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
That's a great question Mohamed. So it will also stop when we reach the end of the string itself. If you look at the source code here: github.com/portfoliocourses/c-example-code/blob/main/reverse_words.c. Specifically, this part here: for (j = 0; i < len; j++, i++) { if (s[i] == ' ' || s[i] == '.') break; temp[j] = s[i]; } the for loop will stop if s[i] is a period or space, but it will *also* stop if i >= len, as the loop will only continue so long as i < len. And we keep incrementing i in the for loop each time, and we keep writing the word into 'temp'. So it should work fine for the last word whether we have "good morning sir." or "good morning sir".
@mohamedelamine1640
@mohamedelamine1640 2 жыл бұрын
@@PortfolioCourses Thank you so much 👌😁
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@mohamedelamine1640 You're welcome Mohamed! :-D
@justcurious1940
@justcurious1940 8 ай бұрын
Great method : // an alternative method without using a temporary array // by keeping track of the first and the last index for each word void reverse_words3(char *string){ bool is_word = false; int first_index = 0, last_index = 0; for(int i = 0; string[i]; i++){ if(string[i] != ' ' && string[i] != '.'){ if(!is_word){ first_index = i; is_word = true; } } else if ((string[i] == ' ' || string[i] == '.')){ if(is_word){ last_index = i-1; is_word = false; while(first_index < last_index){ char temp = string[first_index]; string[first_index] = string[last_index]; string[last_index] = temp; ++first_index; --last_index; } } } } }
@Awesomeman1578
@Awesomeman1578 2 жыл бұрын
What if there is more than one whitespace between every new word?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
If there is a white space character, the inner for loop will immediately break here: if (s[i] == ' ' || s[i] == '.') break; And then the inner while loop will do nothing, and the outer for loop will advance i by 1. The process will just repeat until we hit the next non-space character, so it should work fine in this case.
@mostafamoradi2051
@mostafamoradi2051 3 жыл бұрын
it could be better if the reverse function which has been defined could reverse the place of the words too.... i mean like : yaw eht si siht... anyway. that was good. tnx
@PortfolioCourses
@PortfolioCourses 3 жыл бұрын
So reverse the entire string? I bet that I can make a video on that too, I'll let you know when I post it. :-D In the meantime I have this video where a string is printed in reverse: kzbin.info/www/bejne/n4i0ZqV9jNKejtk
@PortfolioCourses
@PortfolioCourses 3 жыл бұрын
OK, here is a video for creating a function that will reverse the entire string: kzbin.info/www/bejne/hqW2eZalqpWKf9U
Count The Words In A String | C Programming Example
8:08
Portfolio Courses
Рет қаралды 13 М.
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 198 МЛН
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 111 МЛН
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 830 М.
C_69 C Program to Reverse a String | with strrev() and without strrev() function
24:51
Object-Oriented Programming is Embarrassing: 4 Short Examples
28:03
Brian Will
Рет қаралды 2,1 МЛН
15 Years Writing C++ - Advice for new programmers
4:04
SyncMain
Рет қаралды 1,3 МЛН
Counting Occurrences Of A Word In A String | C Programming Example
14:56
Portfolio Courses
Рет қаралды 18 М.
Reverse Words in a String | LeetCode 151 | C++, Java, Python
13:58
Knowledge Center
Рет қаралды 192 М.
you will never ask about pointers again after watching this video
8:03