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 - C Tutorial
From the course: Advanced C Programming: Optimize Performance and Efficiency
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Sort an array6m 39s
-
(Locked)
Challenge: Sort a string24s
-
(Locked)
Solution: Sort a string1m 28s
-
(Locked)
Work with arrays and functions3m 21s
-
(Locked)
Challenge: An array-modification function24s
-
(Locked)
Solution: An array-modification function1m 16s
-
(Locked)
Send a structure to a function4m
-
(Locked)
Challenge: Create a structure function27s
-
(Locked)
Solution: Create a structure function1m 20s
-
(Locked)
Build an array of structures3m 36s
-
(Locked)
Challenge: Sort an array of structures32s
-
(Locked)
Solution: Sort an array of structures1m 7s
-
(Locked)
-
-