A string is stored in array the name of the string is a pointer to the

A string is stored in array the name of the string is

This preview shows page 15 - 22 out of 26 pages.

A string is stored in array, the name of the string is a pointer to the beginning of the string. 15
Image of page 15
Strings If we use one-character string it requires two locations. The difference is shown below, The difference between array and string is shown below, 16
Image of page 16
STRINGS Why do we need null? 17
Image of page 17
String constant (or) Literal String constant is a sequence of characters enclosed in double quotes. When string constants are used in C program, it automatically initializes a null at end of string. Ex:- “Hello” “Welcome” “Welcome to C Lab” 18
Image of page 18
String Input / Output C provides two basic methods to read and write strings. Using formatted input/output functions and using a special set of functions. Formatted input function :- scanf can be used with %s format specification to read a string. Ex:- char name[10]; scanf(“%s”, name); Here don’t use “&” because name of string is a pointer to array. The problem with scanf is that it terminates its input on the first white space it finds. Ex:- NEW YORK Reads only NEW (from above example). 19
Image of page 19
String Input / Output Unformatted input function :- getchar() :- It is used to read a single character from keyboard. Using this function repeatedly we may read entire line of text Ex:- char ch=‟z‟; ch=getchar(); gets() :- It is more convenient method of reading a string of text including blank spaces. Ex:- char line[100]; gets(line); 20
Image of page 20
String Input / Output 2. Formatted output function :- (Writing strings on the output) printf with %s format specifier we can print strings in different formats on to screen. Ex:- char name[10]; printf(“%s”,name); printf(“%0.4”,name);
Image of page 21
Image of page 22

You've reached the end of your free preview.

Want to read all 26 pages?

  • Fall '19

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture