From the course: Advanced C Programming: Optimize Performance and Efficiency

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Solution: Sort a string

Solution: Sort a string

(upbeat music) - [Instructor] For my first solution, I chose to use the fgets function to read in text at line 13. Because you need to have a limit on the sort, the length of the string input is measured at line 14. If you don't do this, then a bunch of garbage characters become part of the sort, as the string doesn't always fill the allocated buffer. The bubble sort is used at line 17, which is my sorting routine. It's pretty much the same routine as shown in an earlier movie. Finally, the sorted string, which is now really just a character array, is displayed. I'm going to build and run this code to show you what it looks like. When you see the string, the first character shown is the new line, which was the last character typed. But it sorted along with the rest of the characters in the string. Then, come all the spaces, and then the letters a to z in uppercase, symbols, finally, lower case. A quick sort can…

Contents