Strings 2025

Strings #

As discussed in the Arrays section, strings in C are stored as an array of characters using the char data type:

1. char name[75];			//Declares an array of characters with 75 elements
2. char name[] = "strings in C";	//Declares an array called name with the elements 'strings in C'

The 1st option above will create an array of 75 chars. The 2nd option above will create an array called name, which has been filled with the characters strings in c.

The C compiler will create an array and populate the array with a sequence of characters as shown below.

NOTE: The compiler automatically appends the null character \0 at the end by default.

Strings stored in Array in C

Text stored in this array is stored as integers, with ASCII codes representing each letter as an integer. The letter s is stored as the decimal value 115. If it was a ‘S’ the decimal value would be 83. ASCII Table

Assigning Values to Strings #

Assigning Values to Strings
As previously mentioned, strings in C are difficult to work with. One example of this is that the assignment operator, = is NOT supported. For example:
1
2
char str[30];
str = "C Strings"; // This is NOT supported in C. We can not directly assing a string to the array.

String Manipulation #

We will sometimes need to manipulate strings to solve an issue. To help with string manipulation C supports pre-defined string functions that are stored in a header file called string.h. Some of the most commonly used funcitons in this header file are:

String Functions

An example of using these is shown below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <string.h>   //Header File that stores the string functions
int main() 
{
	char str1[12] = "Hello";
	char str2[12] = "World";
	char str3[12];
	int len;
	
	/* copy str1 into str3 */
	strcpy(str3, str1);
	printf("strcpy( str3, str1) : %s\n", str3);
	
	/* concatenates str1 and str2 */
	strcat(str1, str2);
	printf("strcat( str1, str2): %s\n", str1);
	
	/* total lenghth of str1 after concatenation */
	len = strlen(str1);
	printf("strlen(str1) : %d\n", len);
	
	return 0;
	}

fgets() and puts() #

fgets() is a function in the stdio.h header file that is used to read in data from the keyboard. This function can be used to over the issue of reading in strings with spaces that is encountered with using scanf(). It is used as follows:


fgets(string_name, sizeof(string_name), stdin); – This will read in a string and store it in the array called string_name.

The sizeof(string_name) will take the size of the array that we defined and will limit the number of characters that can be into the array to that number.

An example of using these is shown below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <stdio.h>
int main() 
{
	char string_name[40];
	
	printf("Enter a string: ");
	fgets(string_name, sizeof(string_name), stdin); // read in the string and store in string_name
	
	printf("The string entered is: ");
	puts(string_name);	//This is a function that will print data to the screen.
	
	return 0;
}

Code Output:

String Output

In the example above, puts() was used to print the string to the screen. The printf() could also have been used to prinf the string to the screen as has been shown previously.

If the user enters more characters than has been defined, the extra characters will be ignore. sizeof() ensures that only the defined number of characters can be entered. This is the reason that fgets() is used instead of another known funciton gets(). The gets() function allows any lenght of characers to be input which could cause a buffer overflow. Buffer overlfows need to be avoided.