Strings in C Programming

Introduction

  • Strings are known as a one dimensional array of characters.
  • A group of characters is called as a character array.
  • The difference between any type of array and the strings is that a character array ends with a '\0' character.

  • Syntax:
    char str_name = “string_name”;

  • If this syntax is taken '\0' is added automatically.
  • A string and a character are different from each other.
  • String is a group of characters. Every element of the string is a character.
  • 1 byte is occupied by every character and the string is stored in a continuous memory location.
  • It is not a compulsion to specify the size of the array when it is being initialized. But just in case you do, '\0' is not counted as its part.

Accessing the strings

  • Let us take an example to demonstrate how strings are being accessed.
  • The data here will be entered in a string character by character and also displayed character by character.

  • #include <stdio.h>
    void main()
    {
         char n[10];
         int i, j;
         for (i=0; j<10; j++)
         {
              printf("\n Enter the character %d",i);
              fflush(stdin);
              scanf("%c", &n[i]);
         }
         for(i=0;j<10;j++)
         printf("%c", n[i]);
    }


  • Strings are used for text manipulations.
  • There are a number of methods which are used for accessing strings.

  • As we are aware that '\0' is a terminating character, block 2 can be simplified by the following code lines:

    while (str_name[i] != '\0')
    printf(“%c”, str_name[i]);

  • A special format specifier, “%s”, is provided by C which is used for the input and display of a string.
  • With the above specifier the entire string can be accessed instead of going character by character.
  • So, in the above code where we have the specifier “%c” we can replace it by “%s” specifier.
  • In this case we would not require counter 'i'.

String Functions

Following are some of the library string functions and their purposes:

FunctionsDescription
StrlenIt finds the length of the string.
StrlwrIt converts the string into lower case.
StruprIt converts the string into upper case.
StrcatIt appends one string at the end of another.
strncatIt appends the first n character of the string at the end of another.
strcpyIt copies a string into another.
strncpyIt copies the first n characters of one string into another.
strcmpIt compares two strings.
strncmpIt compares the first n characters of two strings.

Example

Write a program to demonstrate string function of length, copy and concatenation.

#include <stdio.h>
#include <string.h>
void main ()
{
     char s1[12] = "Tutorial";
     char s2[12] = "Ride";
     char s3[12];
     int  l ;
     /* string copy */
     strcpy(s3, s1);   
     printf("strcpy( s3, s1) :  %s\n", s3 );

     /* string concatenation */
     strcat( s1, s2);   
     printf("strcat( s1, s2):   %s\n", s1 );

     /* string length */
     l = strlen(s1);
     printf("strlen(s1) :  %d\n", l );
}


Output:
strcpy( s3, s1) :  Tutorial
strcat( s1, s2):   TutorialRide
strlen(s1) :  12