7 String utilities

 

It’s often said, and rightly so, that the C programming language lacks a string data type. Such a thing would be nice. It would be easier to guarantee that every string in a program is bona fide and that all string functions work cleanly without flaw. But such claims are untrue. A string in C is a character array, weakly typed, and easy for any C programmer to screw up.

Yes, handy string functions exist in C. A crafty coder can easily cobble together any string function, imitating what’s available in some other, loftier programming language but lacking in C. Still, any creative approach to handling an absent string function in C still must deal with the language’s myopic perception of the string concept. Therefore, some extra training is required, which includes:

  • Reviewing what’s so terrible about strings in C
  • Understanding how string length is measured
  • Creating interesting and useful string functions
  • Building your own string function library
  • Exploring make-believe object-oriented programming

Despite what you can do with strings in C, the grousing and disdain remains—and it’s legitimate. C strings are squishy things. It’s easy to mess up when you create or manipulate a string, even for experienced programmers. Still, strings exist as a valid form of data and are necessary for communications. So, prepare to bolster your string knowledge and build up your programming arsenal.

7.1 Strings in C

7.1.1 Understanding the string

7.1.2 Measuring a string

7.1.3 Reviewing C string functions

7.1.4 Returning versus modifying directly

7.2 String functions galore

7.2.1 Changing case

7.2.2 Reversing a string

7.2.3 Trimming a string

7.2.4 Splitting a string

7.2.5 Inserting one string into another

7.2.6 Counting words in a string

7.2.7 Converting tabs to spaces

7.3 A string library