FALLSEM2013-14_CP2836_24-Sep-2013_RM01_Strings_21 - STRINGS in C string array of characters terminated by NULL character string in\/output printf,S

FALLSEM2013-14_CP2836_24-Sep-2013_RM01_Strings_21 - STRINGS...

This preview shows page 1 - 3 out of 14 pages.

STRINGS in C string: array of characters terminated by NULL character string in/output: printf("%s",S), scanf("%s",S) string.h : collection of functions for string manipulation no standard operators for string assignment and comparisons! (remember: strings are arrays!) string manipulation is based on lexicographical analysis which means lexical order or alphabetical order or dictionary order. String functions in C with examples What are strings? In simple language STRING'S are nothing but the character array.The declaration of string (character array) is much similar to normal array declaration.Each string is terminated by '\0' as indication of string termination.So obviously you will require an extra byte of memory for storing '\0'. '\0' is single character whose ASCII ( American Standard Code for Information Interchange ) value is 0. How to initialize strings (char array)? char site[]={'g','o',o','g','l','e'.}; char Site[]="Programmingspark"; How to take Input as string and print Output that string? Method:1 void main() { char name[30]; printf("Enter your name"); scanf(" %s ",name); //format specifier printf(" %s ",name); } Method:1 void main() { char name[30]; printf("Enter your name"); gets (name); //in-built function puts (name); } Difference between both ways is that 1st way use's format specifier and second way uses in-built function. In 1st way if you Enter something like john karter it will take only john as string and any thing after (space) will be discarded.In second way if you enter any space it will be accepted.Output of second way would be john karter. 1
Image of page 1
Similarity between both way is that you don't have to add '\0' character. scanf(%s ) converts 32(space) or '\n' character into '\0' and gets() converts newline character '\n' into '\0'. Standard C String library functions 1) strlen() :Determines the length of the string Syntax: int strlen(char array) Example: length = strlen(s1) 2) strcpy(); Copies a string from a source to destination Syntax: strcpy (Destination string,source string) Example:char ds[] = “MIT” ; char sr[] “VIT”; strcpy(ds,sr) Output: ds = VIT sr = VIT. 3) strncpy(); Copies character of a string to another string upto the specified lenghth Syntax: strncpy(Destination string, source string, n) where n is the number of character to be copies from source to desitnation Syntax: strcpy (Destination string, Source string, 2) Example:char ds[] = “MIT” ; char sr[] “VIT”; strcpy(ds,sr) Output: ds = VI sr = VIT. 4) strcmp(): Compares characters of two strings (Function discriminates between small and capital letters. Syntax: strcmp (Destination string, Source string) Example:char ds[] = “MIT” ; char sr[] “VIT”; Int c = strcmp(ds, sr) C returns a +ve value indicates ds > sr. 0 value indicates ds and sr are equal -ve value indicates ds < sr. Output, c = -1 5) stricmp(): Compares two strings (Function doesnot discriminate between small and capital letters.
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 14 pages?

  • Winter '15
  • Sundararajan
  • ASCII, getch

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture