Strings in C vs Java

Like Java, we use single quotes ('a') to designate a character , and double quotes ("...") to designate a string. However, C does not have a devoted string type in the same way that Java does (the String class). Instead, you must use char arrays. For example, the following creates a string that holds the word “hello”:

char myGreeting[] = "hello"; // char array used as string

The length of this array will be 6: every string must end with a 0 (the null character), to designate the end of the string. Therefore myGreeting[0] will hold the char 'h' (equivalent to the int 104), myGreeting[1] will hold 'e' (or 101), and so on, until myGreeting[5] holds 0.

Even though strings are really arrays, C has many built-in functions to make their use easier. Most of the functions detailed in this section are defined in the string.h header file, so be sure to #include it!

Inputting a string from the user can be difficult, because the programmer must make absolutely certain that enough memory has been pre-allocated to store it. Not doing so can result in very confusing bugs, since it might overwrite unrelated data. It might even result in a security hole in your program. The easiest way to get a string is with the fgets() function.

Note: You may find online code that uses gets() instead of fgets(). DO NOT USE IT! gets() has serious security issues, that can allow a malicious hacker to gain control of a computer that is running your program. We will talk about this more in the next section.

Here we assume that a constant BUFFER SIZE has already been defined:

char string[BUFFER SIZE]; // temporarily allocate space
printf("Enter a string: ");
fgets(string, BUFFER SIZE, stdin); // input a string

The first argument to fgets() is the array of characters that has been preallocated for the string. There is no need for the & because string is already an array, and can be modified when it’s an argument to a function. The second argument is the maximum size that can be input: the string will automatically be truncated down to the right size to fit in the array, so that nothing gets accidentally overwritten. (Remember, the last element of the array must be 0.) And the third argument is the standard-in stream, stating where to get the information. stdin is defined inside of stdio.h.

There are a number of standard functions made to work with strings. If you wish to use them, be sure to #include <string.h> at the beginning of your code. The simplest is strlen(), which finds the length of a string:

int length = strlen(myGreeting); // length of myGreeting 

This will search the given char array for the first 0, returning its index. So if myGreeting contains “hello”, the above code will return 5. It takes linear time to run, since it must search for the 0.

Use the strcmp() function to compare two strings. This will return 0 if the strings are equal:

if (!strcmp(string1, string2)) { // triggers if identical 

If the strings are not equal, strcmp() returns an int based on the ordering of the two strings. It returns a negative number if the first string comes first, and a positive number if the second string comes first. It does this based on an order that is similar to alphabetical order, but using each char’s numerical value. The comparison will be alphabetical if the strings consist of only lowercase letters. But all numbers come before all letters, and all capital letters come before all lowercase letters.

Note: Look up the “ASCII table” if you wish to see the exact ordering.

strcpy() is used to copy one string into another. Its two arguments are the location to copy the string to, and the original string. For example:

strcpy(copyString, originalString); // copies a string 

Note that you are responsible for making sure that the destination string already has all the space it needs for the data being copied into it! All strcpy() does is take the characters from one string and copy them into the new string. It doesn’t allocate any new space on its own. If you do not do this first, your program will likely overwrite unrelated data or terminate.

For example:

char[strlen(originalString)+1] copyString; // allocation 

Note that the +1 is to make space for the 0 that must be at the end of every string.

The function strcat() is used to concatenate two strings. It looks much like strcpy():

strcat(mainString, suffix); // concatenates suffix to end 

Here, the string suffix is tacked to the end of mainString, permanently altering mainString. As with strcpy(), you are responsible for making sure that mainString has enough space already allocated, for this to be a legitimate operation. If there is not, you may write over unrelated data or cause your program to crash!

Finally, there is a function called sprintf() that can mimic much of the above functionality. sprintf() is identical to printf() detailed above, except that it takes an additional argument before the others indicating a string to write into. It does not print anything to the screen at all:

  sprintf(string, "The value is %d.", value); // saves string

As always, you must make sure that the first string argument contains the space needed, or else risk writing over unrelated data.

Licenses and Attributions

Speak Your Mind