C Strings

What are Strings

A string constant is a one-dimensional array of characters terminated by a null (‘\0’) character. Strings are used to store text information and to perform manipulations on them. Strings are declared in the same manner as other arrays.

For Example

char fruit[10];

When you press any key from the keyboard, then it is said to be a character. But when you press more than one key, it becomes a string. So a combination of characters (group of characters) is called string. “I am a good boy.” is a string. We can print or display the string by using the printf() function as:

printf("\n I am a good boy");

We can print a string by using control string %s or %[..] or %[^] i.e the declaration is as follows:

char name[10];
printf("%s",name);        /*to print a single string*/

or

char name[5][10];
for(i=0;i

Operations on String

String has number of operations in the C-Language, but some commonly used are as follow:

  • Initialization of String variable
  • Reading and Writing of String
  • Combining strings together or Concatenation of two or more than two strings
  • Copy one string to another
  • Comparing two strings
  • Extracting a portion of string or To display a sub-string from the string
  • To reverse a string
  • To find whether a string is palindrome or not

String Handling Functions

C-language is rich in library functions, but to handle or operate some operations with string, we use some powerful string handling functions. All these function are linked with the "string.h" header file strored in the include sub-directory in the Turbo-C compiler. Below the five commonly used string handling function as:

  • strcat()
  • strcmp()
  • strcpy()
  • strlen()
  • strrev()

stcat()

The purpose of this string handling function strcat() is to concatenate or combine two different strings together.

The general syntax used for this is as follows:

strcat(string1,string2);

Example Program:

/*Write a program to concatenate two string*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
  char name1[10], name2[10];
  clrscr();
  printf("Enter the two Names:\n");
  scanf("%s%s",name1,name2);

  strcat(name1,name2);
  printf("%s",name1);
  getch();
}

Output is as follows:

Enter the two Names
Geek
Diary
GeekDiary

stcmp()

The purpose of this function is to compare two strings. It will check which string is alphabetically above the others. For comparison, ASCII (American Standard Code for Information Interchange) values are used.

The general syntax used for this is as follows:

strcmp(string1,string2);

Example Program:

/*Write a program to compare two string*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
  char name1[10], name2[10];
  int i=0;
  clrscr();
  printf("Enter the two Names:\n");
  scanf("%s%s",name1,name2);

  i = strcmp(name1,name2);
  if(i==0)
  {
     printf("Both String are equal");
  }
  else
  {
     printf("Both String are not equal");
  }
  getch();
}

Output is as follows:

Enter the two Names
Geek
Diary
Both String are not equal

strcpy()

The purpose of this function is to copy one string into another string. Note that the target or destination field should be larger than the source field. In other words size of the string1 should be larger to receive the contents of the string2.

The general syntax used for this is as follows:

strcpy(string1,string2);

Example Program:

/*Write a program to copy one string into another string*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
  char name1[20], name2[10];
  clrscr();
  printf("Enter the two Names:\n");
  scanf("%s%s",name1,name2);

  i = strcpy(name1,name2);
  printf("\n Copy String=%s",name1
  getch();
}

Output is as follows:

Enter the two Names
Geek
Diary
Geek Diary

strlen()

The purpose of this function is to count the number of characters in a string i.e. to find the length of the string.

The general syntax used for this is as follows:

n=strlen(string);

Example Program:

/*Write a program to print the length of String*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
  char name[20];
  int len=0;
  clrscr();
  printf("Enter the Name:\n");
  scanf("%s",name);

  len = strlen(name);
  printf("\n Length of String=%d",len
  getch();
}

Output is as follows:

Enter the Name
Diary
Length of String=5

strrev()

The purpose of this function is to reverse a string.

The general syntax used for this is as follows:

strrev(string);

Example Program:

/*Write a program to print the reverse String*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
  char name[20];
  clrscr();
  printf("Enter the Name:\n");
  scanf("%s",name);

  strrev(name);
  printf("\n Reverse String=%d",name
  getch();
}

Output is as follows:

Enter the Name
Geek
keeG
Related Post