FALLSEM2014-15_CP1621_02-Sep-2014_RM02_Character-Array-or-String.pdf - String Definition In C an array of characters is known as a string Declaration of

FALLSEM2014-15_CP1621_02-Sep-2014_RM02_Character-Array-or-String.pdf

This preview shows page 1 - 6 out of 25 pages.

String Definition : In C, an array of characters is known as a string. Declaration of string: char st[80]; The above statement declares a string with 80 characters. Note: The control character „ \ 0‟ (null character) is placed automatically at the end of any string used. String Handling Functions These are the important string handling functions in C. i. strlen() Finds the length of the character string ii. strcpy() Copy a character string to a character variable iii. strcat() Joins to different strings iv. strcmp() Compare two character strings v. strupr() Converts all characters to upper case
Image of page 1
vi. strlwr() - Converts all characters to lower case vii. strrev() Reverse a string Additional String Handling Functions viii. strncmp() Compares the first n characters of two strings ix. strcmpi() Compares two strings with case insensitive x. strncat() Joins a specific number of letters to another string. Note: Returns “0” when two strings are identical otherwise it returns a numerical value which is the difference in ASCII values of the first mismatching characters of the strings being compared. Programs 1. C program to print string This program prints a string. String can be printed by using various functions such as printf, puts. #include<stdio.h> main() {
Image of page 2
Image of page 3
2. String length This program prints length of string, for example consider the string "c programming" it's length is 13. Note: Null character is not counted when calculating string length. To find string length we use strlen function of string.h. C program to find string length #include<stdio.h> #include<string.h> main() { char a[100]; int length; printf("Enter a string to calculate it's length\n"); gets(a); length = strlen(a); printf("Length of entered string is = %d\n",length);
Image of page 4
,
Image of page 5
Image of page 6

You've reached the end of your free preview.

Want to read all 25 pages?

  • Spring '19
  • ASCII

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture