Responsive Ads Here

String in C

A string is an array of characters. The end of the string is marked with a special character, the null character, which is simply represented by '\0'. Strings are enclosed by double quotes whereas, character is enclosed by single quotes always.

Declaration :

Strings are array of char data type. It is declared as follow :

           char s[5] ;
                                      Declaration of strings in C language
                       

Initialization :     

      char message [20] = "Hello world !";
      
      char message [20] = {'H', 'e', 'l', 'l', 'o', '\0'};

      char message [] = "Hello!"

when we declare "char message[20]”, 20 bytes of memory space is allocated for the string value. And when we declare "char message[]”, memory space will be allocated as per the requirement during execution time of program.

Example1 :

#include<stdio.h>
int main()
{
      char message[10] = "Hello!";
      char message1[] = " world" ;
      printf("%s, %s, message, message1);
      return 0;
}

Output :

Hello! world


Example2:

#include<stdio.h>
int main()
{
      char value[];
      printf("Enter any string : ");
      scanf("%s",&value);
      printf("Entered String = %s ", value);
      return 0;
}

Output :

Enter any string :
GoProgramming
Entered String = GoProgramming

scanf() function doesn't control the value with space. For this we use gets() instead of scanf().

gets(variable);

Example3:

#include<stdio.h>
int main()
{
      char value[];
      printf("Enter any string : ");
      gets(value);
      printf("Entered String = %s ", value);
      return 0;
}

Output :

Enter any string :
Go Programming
Entered String = Go Programming


String Handling Functions :

Sometimes, we need to manipulate string in our program according to our needs. For this C programming language provide us some in-built String Manipulation Functions which are available in <string.h> header file.
These functions are listed below :

  • strlen() :

strlen() function is used to count the number of characters in a string.

Example :

#include<stdio.h>
#include<string.h>
int main()
{
      char value[] = "Hello World";
      int length;

      length = strlen(value);

      printf("String length = %d", length);
      
      return 0;
}

Output :

String length = 11

  • strrev()

It reverses the given string.

Example :

#include<stdio.h>
#include<string.h>
int main()
{

      char value[] = "Hello";
      char value1[];

      length = strrev(value);

      printf("String  = %s", value);
      printf("Reversed String = %s, value1);
      
      return 0;

}

Output :


String = Hello World

Reversed String = olleH

  • strcpy()

This function copies a string value into another string.

Example :


#include<stdio.h>
#include<string.h>
int main()
{


      char value[] = "Hello";
      char value1[] = "World";
      char value2[];


      strcpy(value2, value);
      strcpy(value2, value1);

      printf("String 1 = %s", value);
      printf("String 2 = %s", value1);
      printf("New String = %s", value2);
      
      return 0;
}


Output :


String 1 = Hello
String 2 = World
New String = HelloWorld



  • strcmp() :
strcmp() function is used to compare two strings. If both strings are equal then function will return value zero(0). If first string is greater than second than function return value >0 otherwise  <0. This function is case sensitive.

Example :


#include <stdio.h>
#include <string.h>

int main()
{
    char str1[] = "abcd", 
            str2[] = "abCd", 
          
    int result;

    // comparing strings str1 and str2
    result = strcmp(str1, str2);
     
    if(result < 0)
    {
       printf("str1 is less than str2");
    }
    else if(result > 0) 
    {
       printf("str2 is less than str1");
    }
    else 
    {
        printf("str1 is equal to str2");
    }

    return 0;
}

Output :

str2 is less than str1

  • strcat()


This function  is used  to concatenate two or more strings.

Syntax :

      strcat(str1, str2);
      strcat(str1, strcat(str2,str3));