Using Strings in C with Examples

Strings are just a one-dimensional collection of characters with the null character ‘0’ at the end. On the other hand, a null-terminated string includes the characters that make up the string, succeeded by a zero. In this article, you will learn about how to deal with strings in Ubuntu 20.02 Linux system. Make sure you have GCC compiler installed in the system. Now open the terminal via Ctrl+Alt+T to get started.

Example 01:

Our first example will show us the first method about how to declare the string in C language open the command-line shell via “Ctrl+Alt+T” from the keyboard first. After the shell has been opened, create a C-type file using the “touch” instruction below. We have named the file “test.c”.

$ touch test.c

To open this newly created C file as nano editor, execute the below “nano” command in the terminal.

$ nano test.c

Now, you can see the file has been opened in GNU Nano editor 4.8. Write out the below C script in it. This script contains the header library package at the first line of code. Without this library, we cannot make our code executable. Then we have created the main function to add our execution code to it. We have declared a string “a” which is a sequence of characters. We have assigned it a value “Linux” at the same line of code. In the following line, we have printed it via the “printf” statement, and the function has been closed.

Char a[] = “Linux”

To make our C file executable, run the gcc compiler along with the name of a file “test.c”. Make sure you have GCC configured on Ubuntu 20.04. Otherwise, your code will never get compiled. The below command works for the compilation of a code as below.

$ gcc test.c

When you find no errors after compiling the code, this means your code is correct. Hence, execute the file by “./a.out” query in the shell as below. You can see the output shows the result of a string “a” as “Linux”.

$ ./a.out

Example 02:

Another method to declare and use a string in our C code is by mentioning the size of a string within brackets. Hence, open the same “test.c” file again with a Nano Editor.

$ nano test.c

The file has been opened in GNU editor. You can see we have added the below C script in our file. Everything is the same in the code except the mentioning of a size. We have given a size of 10 to a character type string “a” and assigned it a long value with more than 10 characters. This means this program should output an error. Let’s check that after saving this file by Ctrl+S and leave it via Ctrl+X.

Compile the file test.c using the gcc compiler as below. You will get an error that the string of the array is way too long than the initial size.

$ gcc test.c

Let’s open the file again and correct the code.

$ nano test.c

After opening the file in nano editor, we have updated its size up to 20. Save and quit the file to see the results.

Again compile the code via gcc and then run it via the a.out command in the shell. You can see it works fine with the correct size defined.

$ gcc test.c

$ ./a.out

Example 03:

In this example, we will look at two new ways to define string-type variables. So, open the file “test.c” once again.

$ nano test.c

Now the file has been opened. We have included a library for standard input/output. After that, the main() function has been started. In the main function, we have defined two-character type string arrays a[] and b[]. Both the variables got the same values. Both strings have been printed out via the “printf” statement, and the main method closes. Save your file and exit.

Now compile the code with GCC compiler once again and then execute it. You can see the output is successfully printing both string-type array values in the terminal.

$ gcc test.c

$ ./a.out

Example 04:

In this example, we will look at whether we can define a value to a string-type array variable at the next line. Let’s make another file “new.c” to add a C script to it. After creating it, open it via GNU nano editor as well.

$ touch new.c

$ nano new.c

Now update your code with the below-presented code in the file. We have defined the main method, and in this method, we have initialized character type string having size 5. In the next line, the string has been assigned with a value “Linux” and then the string has been printed out via the “printf” statement at the next subsequent line. Save your code and leave the file.

Upon compilation of the file “new.c”, it gives an error that the assignment cannot be done with such sort of expression with an array type. This means to assign a value; we have to directly do it on the line where the string is defined.

$ gcc new.c

Example 05:

This example is about initializing a string type array and then assigned it a value inputted by a user at run time. So, open the file “new.c” via terminal.

$ nano new.c

After the opening of a file, please copy the below code in it. This code contains the same header file and main function. A string of character type array has been defined with size 50. Print statement has included asking a user to add input. The Scanf() method is used to get a user input at run time and save this inputted string type value to a string “str”. Once again, the printf statement is being used to print the user inputted value in the terminal, and the main method is closed. Save and quit the nano editor.

Again, compile and then run the code as per the same queries. The execution will ask for user input. We have added “AqsaYasin” as a string value. At the next line, it is printing our inputted value.

$ gcc new.c

$ ./a.out

Example 06:

Let’s apply some other function of a string-type variable. Open your file once again. The overall coding is the same. Just add up some more functions in it. To read one row of string, we have just used the fgets() method. You may also use puts() to display the string. We have used the sizeof() method to get the size of a string added by a user.

The compilation and execution are showing the user inputted string.

Example 07:

Open the file to see some more features on strings. This time we have been passing a string as an argument to another function, Show().

The execution shows the inputted value by a user and printing it via the main method and then a Show() method.

Conclusion:

We have elaborated on all the related examples of strings in this article. We hope it will be helpful while studying strings at your end.



from Linux Hint https://ift.tt/36weP60

Post a Comment

0 Comments