17688_Lecture 13.pdf - STRINGS BY RICHA JAIN Strings \u2022 Just like group of integers can be stored in an integer array similarly a group of characters

17688_Lecture 13.pdf - STRINGS BY RICHA JAIN Strings u2022...

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

STRINGS BY: RICHA JAIN
Image of page 1
Strings Just like group of integers can be stored in an integer array, similarly, a group of characters can also be stored in a character array. This array of characters is known as a string. Strings are used to used for manipulating and storing text such as words and sentences.
Image of page 2
Fundamentals of strings Strings are arrays of characters String is a pointer to first character (like array) Value of string is the address of first character Each element of the string is stored in a contiguous memory locations. Terminated by a null character (‘ \ 0’) which is automatically inserted by the compiler to indicate the end of string .
Image of page 3
A string is always enclosed in double quotes and may include characters such as alphabets, numbers, escape sequence , blank space and special characters. Eg : “Good morning” , “41 - B”, “121+31 - 43”, ”B” “ “
Image of page 4
String Definition They are defined as char array_name[size]; e.g. char carname[30]; or char *carname; It defines an array name and reserves 30 bytes for storing characters and single character consumes 1 bytes each. Since the last byte is used for storing null character so total number of character specified by the user cannot exceed 29 .
Image of page 5
String Initialization String Initialization Two ways: Define as a character array or a variable of type char * char color[] = "blue"; //char array Or char color[] = { 'b', 'l', 'u', 'e', '\0' }; char *colorPtr = "blue"; //pointer variable Remember that strings represented as character arrays end with '\0' color has 5 elements b l u e \0 color b l u e \0 *colorPtr Temporary
Image of page 6
char *colorPtr = "blue"; //pointer variable printf(“%s”, colorPtr); Is correct way to use pointer to char. But following is wrong: char *colorPtr; //pointer variable scanf(“%s”, &colorPtr); /* invalid statement %s don’t work with pointer to char */
Image of page 7
Strings in C(Reading and Writing a string) #include<stdio.h> #include<conio.h> int main() { char name[20]; printf("enter the name of the person"); scanf("%s",&name); printf("enter name is %s",name); getch(); return 0; }
Image of page 8
How?
Image of page 9
Image of page 10

You've reached the end of your free preview.

Want to read all 29 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture