In my previous article, I have written about arrays in C, what purpose they serve and different types of arrays. But arrays only contain numeric figures leaving us a question behind, is there no such method to intake characters? Let's find that out in this article.

Strings in C
Strings in C

Overview:


Definition of string

Strings are basically a one-dimensional array of characters that ends at a null character represented by '\0'.



Declaration of strings

Declaring a string is similar to declaring a one-dimensional array. The basic syntax of declaration follows as under:

char string_name[string_size];

In the above syntax string_name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store. Please keep in mind that there is an extra terminating character which is the Null character (‘\0’) used to indicate termination of string which differs strings from normal character arrays.

For a quick example, if you want to declare the word "TechGyd" in the form of a string:

char blogname[8]={'T','e','c','h','G','y','d','\0'};

Initialization of string

A string can be initialized in several ways. Lets look at the following example to understand this. Below is an example to declare a string with the name as demo and initialize it with “TechGyd”.

1. char demo[] = "TechGyd";

2. char demo[20] = "Techgyd";

3. char demo[] = {'T','e','c','h','G','y','d','\0'};

4. char demo[8] = {'T','e','c','h','G','y','d','\0'};



Following is the memory representation of a string “TechGyd”.

Memory Representation of strings
Memory Representation of strings

String Functions

Since strings are character arrays, so we can pass strings to function in a likewise we pass an array to a function. C supports several functions that work on null-terminated strings:


Sr.No.FunctionPurpose
1strcpy(str1, str2);Copies string str2 into string str1.
2strcat(str1, str2);Concatenates string str2 at the end of string str1.
3strlen(str1);Returns the length of string s1.
4strcmp(str1, str2);Returns 0 if str1 and str2 are the same; less than 0 if str1<str2; greater than 0 if str1>str2.
5strchr(str1, chr);Returns a pointer to the first occurrence of character chr in string str1.
6strstr(str1, str2);Returns a pointer to the first occurrence of string str2 in string str1.

How do they work? Let's check in the following program code:

Program using C Strings

Here is a sample program involving all string functions mentioned above:

#include<stdio.h>
#include<string.h>
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;
}

Output:

Strings Function Output
Strings Function Output

All programs written in this post are compiled online.

Books I Prefer:



  

I hope this article was helpful to understand strings in C! Comment below, if you've got any question. Head back soon for another interesting article on C Programming.