String in C/C++ programming

<<Previous

Next >>





String is defined as the group of characters, digits and symbols enclosed within quotation marks. They are always declared as one-dimensional character arrays. Strings are always enclosed within double quotes whereas character is always enclosed with single quotes. Each string is terminated with the ‘\0’(NULL) character.

For example:
 char name[]={‘I’,‘N’,‘D’,‘I’,‘A’,‘\0’};

Each character of string occupies one byte of memory. They are stored in contiguous memory location.

For example:



The %s is used as the format specifier to display the string.

Declaration and Initialization of String

There are two ways in which the string is declared that are character array and string literal or string constant.

For example:
char name[]={‘I’,‘N’,‘D’,‘I’,‘A’,‘\0’}; //using char array
 or
char name[]="INDIA"; //using string literal

While declaring strings, you must ensure that the size must be equal to the number of characters plus NULL character.

For example:
char name[5]={‘I’,‘N’,‘D’,‘I’,‘A’,‘\0’}; //This is wrong

char name[6]={‘I’,‘N’,‘D’,‘I’,‘A’,‘\0’}; //This is correct

Let's see the following example program to display the string.

#include<stdio.h>

#include<string.h>


main()
{
  char string[6]="INDIA";
  char name[8]={’W’,‘E’,‘L’,‘C’, ‘O’,‘M’,‘E’};

  printf("our Country is %s",string);
  printf("\n %s",name);
  return(0);
}

Output:

our country is INDIA
WELCOME

In C Programming, the string.h header file contains all the string functions that are used to manipulating strings. Next section we explain some of the string functions.


<< Previous

Next >>




strcat() - String Concatenation        strcmp() - String Compare


strcpy() - String Copy        strlen() - String Length














String in C/C++ programming

<<Previous

Next >>





String is defined as the group of characters, digits and symbols enclosed within quotation marks. They are always declared as one-dimensional character arrays. Strings are always enclosed within double quotes whereas character is always enclosed with single quotes. Each string is terminated with the ‘\0’(NULL) character.

For example:
 char name[]={‘I’,‘N’,‘D’,‘I’,‘A’,‘\0’};

Each character of string occupies one byte of memory. They are stored in contiguous memory location.

For example:



The %s is used as the format specifier to display the string.

Declaration and Initialization of String

There are two ways in which the string is declared that are character array and string literal or string constant.

For example:
char name[]={‘I’,‘N’,‘D’,‘I’,‘A’,‘\0’}; //using char array
 or
char name[]="INDIA"; //using string literal

While declaring strings, you must ensure that the size must be equal to the number of characters plus NULL character.

For example:
char name[5]={‘I’,‘N’,‘D’,‘I’,‘A’,‘\0’}; //This is wrong

char name[6]={‘I’,‘N’,‘D’,‘I’,‘A’,‘\0’}; //This is correct

Let's see the following example program to display the string.

#include<stdio.h>

#include<string.h>


main()
{
  char string[6]="INDIA";
  char name[8]={’W’,‘E’,‘L’,‘C’, ‘O’,‘M’,‘E’};

  printf("our Country is %s",string);
  printf("\n %s",name);
  return(0);
}

Output:

our country is INDIA
WELCOME

In C Programming, the string.h header file contains all the string functions that are used to manipulating strings. Next section we explain some of the string functions.


<< Previous

Next >>






strncat() - String n Concatenation        strlwr() - String Lower       

strncmp() - String n Compare       strncpy() - String n Copy