String Manipulations :

String in C language :

In C language, an array of characters is known as a string. Consider the following example to declare a string :

char st[50]

This statement declares a strings array with 50 characters. The control character '\0' which represents a null character is placed automatically at the end of any string used.

Example :

char name[5] = {'A', 'y', 'a', 'N', '\0'};

String handling functions :

There are four important string handling functions in C Language.

  1. strlen() function
  2. strcpy() function
  3. strcat() function
  4. strcmp() function

strlen() function :

strlen() function is used to find the length of a character string.

Syntax :

  1. int n;
  2. char st[20]="Bangalore";
  3. n = strlen(st);

This will return length of the string 9 which is assigned to an integer variable n.
Note : That the null character '\0' available at the end of a string is not counted.


strcpy() function :

strcpy() function is used to copy a character variable.

Syntax :

  1. char city[15];
  2. strcpy(city,"BANGALORE")

This will assign the string "BANGALORE" to the character variable city.
Note : That character value like city = "BANGALORE"; cannot be assign in C language.


strcat() function :

strcat() function is used to join character stings. When two character strings are joined, it is referred as concatenation of strings.

Syntax :

  1. char city[20]="BERHAMPORE";
  2. char pin[8]="742103";
  3. strcat(city,pin);

This will join the two strings and store the result in city as "BERHAMPORE742103".
Note : That the resultant string is always stored in the left side string variables.


strcmp() function :

strcmp() function is used to compare two character strings.

Syntax :

  1. char city[20]="MADRAS";
  2. char town[20]="MANGALORE";
  3. strcmp(city,town);

This will return an integer value - 10 which is the difference in the ASCII values of the first mismatching letters 'B' and 'N'.
Note : That the integer value obtained as the difference may be assigned to an integer variable as follows :

int n;
n = strcmp(city,town);


<string.h> :

<string.h> is header in the C standard library for the C language which help us for handling string functions and memory functions.

Example :

  1. /* Description: write a program to use string functions */
  2. #include<stdio.h>
  3. #include<strings.h>
  4. main()
  5. {
  6. int n;
  7. char name[20],age[20],temp[20],verify[5]="AyaN";
  8. puts("Enter verified user Name : ");
  9. gets(name);
  10. puts("Enter verified user Age : ");
  11. gets(age);
  12. n = strlen(name);
  13. printf("\nName Length is - %d character.\n",n);
  14. strcpy(temp,name);
  15. puts(temp);
  16. puts("Name Copy Completed.\n");
  17. strcat(name,age);
  18. puts(name);
  19. puts("Name and age join completed.\n");
  20. if(strcmp(verify,temp)=='\0')
  21. {
  22. puts(name);
  23. puts("This user is verified.");
  24. }
  25. else
  26. {
  27. puts(name);
  28. puts("This user is not verified.");
  29. }
  30. getch();
  31. }

Note : gets() use for read strings (Data input) and puts() use for write strings (Data output).

Output :

c language - write a program to use string functions

Next Update

Loading...

Currently we improved our website User Interface and other contents. Our next update is adding video tutorials on this site so, We create a new YouTube channel, our YouTube channel name is CodeX.

We need some time for make video tutorials (CodeX) so, stay with us and keep supporting and you know already we make a Collaboration with HackIsOn YouTube channel long time ago So, please Subscribe Our Channels