Reverse The Words In A String | C Programming Example

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ก.พ. 2025
  • An example of how to reverse the words in a string with C. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!

ความคิดเห็น • 18

  • @durkaperiasamy2405
    @durkaperiasamy2405 ปีที่แล้ว +4

    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 3 หลายเดือนก่อน

      Did You?

  • @ManikantaSaiTejaMathi
    @ManikantaSaiTejaMathi 3 หลายเดือนก่อน

    Nice set of programs

  • @warplanner8852
    @warplanner8852 10 หลายเดือนก่อน +1

    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.

  • @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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@PortfolioCourses Thank you so much 👌😁

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว +1

      @@mohamedelamine1640 You're welcome Mohamed! :-D

  • @madhubabu3482
    @madhubabu3482 2 ปีที่แล้ว

    The occurences or frequency of words in a string. Please do it.

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว +1

      Is this video what you're looking for Madhu? :-) Video: th-cam.com/video/Zydf9p1VOGU/w-d-xo.html

  • @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.

  • @justcurious1940
    @justcurious1940 10 หลายเดือนก่อน +1

    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;
    }
    }
    }
    }
    }

  • @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 ปีที่แล้ว +1

      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: th-cam.com/video/iWQ5rGWmkXw/w-d-xo.html

    • @PortfolioCourses
      @PortfolioCourses  3 ปีที่แล้ว +4

      OK, here is a video for creating a function that will reverse the entire string: th-cam.com/video/PtSHcou0WIs/w-d-xo.html