Strings in C Language – Create, Access, and Modify Strings

Spread the love
strings-in-c-programming-language-with-example-programs

Introduction:

We have looked at the Arrays and multi-dimensional arrays in earlier articles. In today’s article, We will look at the Strings in C programming language. How to Create, access, and modify the strings in C.

Strings in C Language:

C Language doesn’t have any special datatype to represent the strings. Strings in C programming language are one-dimensional character arrays. All strings in the C language will terminate with the NULL ( \0) character.

We can create the strings in C by using the double quotes ( "). The strings are a sequence of characters that are enclosed within the double quotes ( ")

Here are a few examples of the Strings in C language:

"Welcome to SillyCodes.com"

"Leo Messi"

"Programming is Fun"

we can’t create strings using single quotes( '). The single quotes are used to represent the characters in C language. So 'v' is a single character ( Not a String ).

📢 Strings in C programming must end with the NULL ( \0) Character

As the strings are a character array, each character size is one byte, So the size of the string is equal to the number of characters in the string plus the NULL character.

The characters of the strings are stored in the continuous memory address.

Here is the graphical representation of the C Strings.

memory-layout-of-strings-in-c

As you can see from the above memory layout diagram, Each character in the array is of one byte and all characters of the string are stored in continuous memory.

The NULL Character( \0) is a special character and its ASCII Value is equal to Zero(0).

Declaring Strings in C Language:

As specified earlier, The strings are character arrays. To create strings use the character arrays.

The Syntax for declaring the strings in c:

Here the string_name is the name of the string and string_size is the maximum number of characters string can hold including the NULL character. Let’s look at an example.

String Declaration Example:

In the above example, We have created a String called name with the size of 20 characters. So the name string can store a name with a max size of 19 letters (As the string needs to be terminated with the NULLcharacter)

Initialization of Strings in C:

Similar to the C Arrays and variables, We can initialize the String with the desired values.

We can initialize the strings in different ways, Let’s look at them

String initialization using the character Array:

We can initialize the string using a character array, But we must need to end the character array with the NULL character. If we are initializing the array, Then specifying the string size is optional. (similar to Array initialization)

String Initialization using the String Literal:

We can use the string literals to initialize the string like below.

If we use the string Literals to initialize the string, Then the compiler automatically places the NULL character ( \0) at the end of the string.

So The above string has 5 characters R, o, s, s, \0.

Make sure to specify the string size as per your requirement properly.

📢If we initialize the string, Then the sizeis optional.

Accessing the String Elements in C:

As strings are character arrays, We can access the string elements (Characters) using the subscript

[ ] operation. Have a look at the following program.

In the above example, We have initialized a string named message with the Welcome string literal.

Then to access the first element of the string, We used the subscript operator with the element index. which is message[0], and stored the result in the ch variable.

📢Similar to the Arrays, Strings are Also Zero Indexed. So the first element of the string will be stored at the index

Read and Print Strings in the C Programming:

Let’s look at how to read and print the strings in C programming language.

Reading the strings from the user:

We can use the scanf function with the %s format specifier to read the string values from the user.

The scanf() function reads the string up to the whitespace character. The whitespace character can be any character like Space ( ' '), Tab ( '\t'), New Line ( '\n'), and carriage return ( '\r'), etc.

📢Learn more about the white spaces at the Character Set of C Tutorial

Here is an example to read a string from the user using the scanf() function

Printing the strings on the Console:

We use the printf function with the %s format specifier to print the string in the c programming language.

We can also use the puts() function to print the strings on the console.

Program to Read a string from user and print string on console:

Here’s an example program that uses the scanf() and printf() functions to read the user’s input string and print it back to the console.

Program Output:

Compile and run the program

read-and-print-strings-in-c-program-output

In the above program output, The user provided the input string as Welcome, We used the scanf function to read the Welcome string literal and stored it in the variable called message

scanf("%s", message");

Then We used the printf() function displays the message on the console like below.

printf("Given String is : %s \n", message);

As specified earlier, The scanf() function can only read the strings up to a Whitespace character, So if your string contains any spaces or tables, Then only the string up to the space or tab will be read by the scanf function.

Let’s look at this example

In the above output, The scanf function only read the string Welcome, and skipped the input string “ to Sillycodes.com”. This is happened due to the space character.

In order to read the complete line with spaces, We need to use the gets() function.

Reading a Line (string with spaces) using the gets() function:

The  gets() function is helpful to read a line from the standard input. The reading stops if a new line or EOF is encountered.

📢The  gets() function can read multiple strings. The space won’t stop the gets reading, Only the  Newline or  EOF stops.

Here is a program to read a line from the user ( string with spaces)

Program Output:

Now we are able to read the complete line from the user. We have looked at the gets function in detail in the Standard Input and Standard Output Functions in C Program.

📢The getsfunction won’t do any Boundary-checks for the provided buffer. So it is advised to use it with caution or you can use functions like fgetsand getlinefunctions to read the string from the user.

Using fgets() function to read the strings with white spaces.

Passing String to Functions:

We can pass the string to a function as an Array of characters or as a pointer to characters.

Here is a program to pass the string to function as an Array of characters

Example Program: How to Pass Strings to Functions in C Language:

The following program will prompt the user to provide the name and calls a function called greetUser with the name as the parameter. ( Passing string name to function greetUser)

The greetUser() function takes a character array ( char name[]) as a formal argument. Then it will display a welcome message on the console.

Program Output:

Let’s compile and run the program and observe the output.

As you can see from the above output, the user entered the nameas the Joey and the main() function called the greetUser(name) function with the name, and greetUser() function received the name as the character array and printed the welcome message on the screen.

String Functions:

The C Programming language comes with a few string manipulation and utility functions. Here are a few string functions in the C.

Function NameDescription
strlen()To calculate the length of the string
strcpy()To copy a string to another string
strcmp()To compare two strings
strcat()To concatenate two string
strchr()To search for the first occurrence of a character in the given string
strstr()To search for the first occurrence of a string in the given string

We are going to look at the above string functions in detail in the upcoming articles.

Conclusion:

We have looked at the Strings in C programming language and how to declare, Initialize and modify strings with example programs. We also learned about how to read and print the strings in C programming and finally, we looked at how to pass strings to the functions.

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

14 Responses

  1. […] our previous article, We looked at the strings in the C Language and how to read and print the string in C. In today’s article, We are going to look at the strlen […]

  2. […] have looked at the introduction to strings in C and strlen library function in earlier articles, In today’s article, We will look at the strcpy […]

  3. […] Strings in C Language – Declare, Initialize, and Modify Strings […]

  4. […] Strings in C Language – Declare, Initialize, and Modify Strings […]

  5. […] a Program to Copy String in C programming language. The program should accept a string (character array) from the user and copy it to new […]

  6. […] Strings in C Language – Declare, Initialize, and Modify Strings […]

  7. […] Strings in C Language – Declare, Initialize, and Modify Strings […]

  8. […] Strings in C Language – Declare, Initialize, and Modify Strings […]

  9. […] Strings in C Language – Declare, Initialize, and Modify Strings […]

  10. […] Strings in C Language – Declare, Initialize, and Modify Strings […]

  11. […] Strings in C Language – Declare, Initialize, and Modify Strings […]

  12. […] is recommended to know the C-Strings, C-Arrays, and Functions in C to better understand the following […]

  13. […] is recommended to know the basics of the C-Strings, C-Arrays, and Functions in C to better understand the following […]

  14. […] have looked at the Strings in C language in earlier articles, In today’s article, We will learn about the Pointers in C programming. We […]

Leave a Reply