Learn Various Aspects of String in C Programming

0
195
ASpects of String in C programming

A string in C programming is nothing but an array or sequence of characters. These characters can be anything like numbers, alphabets, or special characters. Strings are terminated by the use of a null character represented by ‘\0’.

For instance, Trickyedu.com

The above string can be represented as:

T r i c k y e d u . c o m \0

How to Declare String in C Programming? 

Declaration of a string in C follows the given syntax,

char string_name[size_of_string];

string_name represents the name given to the string by the programmer and size_of_string represents the number of characters a string can hold.

It is a numerical value. This includes an extra character ‘\0’,  which is a null character and is used to terminate the string.

String Initialization in C

There are a couple of ways by which string initialization can be done in C. They can be better understood with the help of a sample program which is given below.

#include<stdio.h>

int main()

{

char str1[] = “Hello”; //If You initialize string like this then memory is allocated at the execution of the program.

char str2[10] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’}; //If you want to initialize string in C programming like this then 10 bytes of memory is reserved.

printf(“%s\n”, str1);

printf(“%s\n”, str2);

return 0;

}

Output

Hello

Hello

So as you can observe in the above program, both the methods printed the given string in the same way. But it is ideal to use the first method as it is faster and efficient. The access specifier used for strings in c is %s.

A string can also be input from the user using scanf. It is very easy to do this. Let us take a look at a sample code.

#include<stdio.h>

int main()

{

char str1[20]; //Declare str1 string in C programming

printf(“Enter the string\n”);

scanf(“%s”, &str1);

printf(“This is the entered string: %s\n”,str1);

return 0;

}

Output

Enter the string

Hello

This is the entered string: Hello

Here, mentioning the size of the string becomes really important.

What is the Difference Between Array and String in C Programming?

The basic difference between arrays and strings is that arrays can consist of all data types and they can be of any length while strings consist of the ASCII characters while being of a certain length, being terminated by a special null character represented by \0.

Arrays are nothing but special variables that store some data inside of them in a set, meaningful sequence. Whereas string is a single data item that consists of some characters and is terminated using null (‘\0’) characters.

An array that comprises several strings referred to as an ‘Array of Strings’ can also be easily constructed.

There are various string functions in the library of C and they are very useful. Here is a list of some of these functions along with their use.

String Functions in C Library

The functions mentioned below are stored in “string.h” header file. So, If you want to use these functions like strlen, strcmp, strcpy, etc. then you should include “string.h” header file at the beginning of the program.

Name of Function Use of Function
Strlen This function is used to find the length of a given string.
strlwr It converts the uppercase string letters into lowercase letters.
strupr This function converts the lowercase string letters into uppercase letters.
strcat It appends one string at the end of another string.
strncat This function appends the first n characters of a string into another.
strcpy It copies the content of one string into another
strncpy This function copy the first n characters of one string into another.
strcmp It compares two different string in C Programming.
strncmp This function is used to compare the first n characters of a string with another string.
strcmpi It compares two strings with each other irrespective of the case of letters.
stricmp This function is used to compare two strings with each other while taking into account the case of letters.
strrev It reverses a string.
strchr This function finds the first time a given character occurs in a string.
strrchr This function is used to find the last time a given character occurs in a string.

How to Reverse a String in C Programming?

Strrev function helps you to reverse a string in C. Let us understand it’s syntax and working using an programming example.

#include<stdio.h>

#include<string.h>

int main()

{

char str1[20] = “Hello People”; //Initialize string str1

printf(“The string is =%s\n”,str1);

printf(“The reversed string is =%s\n”,strrev(str1));

return 0;

}

Output

The string is =Hello People

The reversed string is =elpoep olleh

Sample Code For Getting C String From User.

#include <stdio.h>

#include <string.h>

int main()

{

char str1[20];

puts(“What is your name?\n”);

gets(str1);

puts(str1);

return 0;

}

Output

What is your name?

Trickyedu

Trickyedu

This code uses the gets() and puts() functions to get the input from the user and print it to the screen. Remember that it is really important to include the string.h header file. The functioning of puts() function is similar to printf() function. It displays the string on the output screen. While the gets() function is to read the string with whitespace.

Click Here to Learn What is the Difference Between Strlen and Sizeof function.

 

 

 

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here