Strings - Strings Definition Group of characters digits and symbols enclosed within quotation marks are called as strings String is always declared as

Strings - Strings Definition Group of characters digits and...

This preview shows page 1 - 10 out of 28 pages.

Strings
Image of page 1
Definition Group of characters , digits , and symbols enclosed within quotation marks are called as strings. String is always declared as character array. Strings are normally used to manipulate the text such as words and sentences. Every string is terminated with the ‘\0’ NULL character. The NULL character is a byte with all bits at logic zero. Its decimal value is zero.
Image of page 2
Example char name[]={‘I’,’N’,’D’,’I’,’A’,’\0’}; Each character of the string occupies 1 byte of memory. The last character is always ‘\0’ (NULL). It is not compulsory to write ‘\0’ in a string. The characters of a string are stored in contiguous memory locations.
Image of page 3
Memory map of string I I N D A \0
Image of page 4
Declaration and initialization of string char name[]=“INDIA”; The C compiler inserts the NULL character automatically at the end of the string . So the initialization of NULL character is not essential. The NULL character must be included in the array size. Strings are stored in memory as ASCII codes of characters.
Image of page 5
Example #include<stdio.h> #include<conio.h> void main() { char name1[7]={'C','O','U','N','T','R','Y'}; char name2[8]={'C','O','U','N','T','R','Y'}; clrscr(); printf("\n Name1 = %s", name1); printf("\n Name2 = %s", name2); getch(); }
Image of page 6
Display of strings with different formats #include<stdio.h> #include<conio.h> void main() { char name1[8]={'C','O','U','N','T','R','Y'}; clrscr(); printf("\n %s",name1); printf("\n %.5s",name1); printf("\n %.8s",name1); printf("\n %.15s",name1); printf("\n %-10.4s",name1); printf("\n %10.4s",name1); printf("\n %11s",name1); getch(); }
Image of page 7
String standard functions strlen() Determines the length of a string
Image of page 8
Example #include<stdio.h> #include<conio.h> #include<string.h>
Image of page 9
Image of page 10

You've reached the end of your free preview.

Want to read all 28 pages?

  • Summer '12
  • Ravi
  • ASCII, getch, null character, clrscr

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture