{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lect20 - DS CS 11002 Computer Sc Engg IIT Kharagpur $ 1...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 & $ % Programming with String Lect 20 Goutam Biswas
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 & $ % A string of Characters A string of characters is a sequence of data of type char (the ASCII codes) stored in consecutive memory loactions and terminated by the null character ’ \ 0 ’ (the ASCII value is 0). The null string (of length zero (0)) is the null character only. Lect 20 Goutam Biswas
Image of page 2
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3 & $ % An Example · · · A string constant is written within a pair of double quotes . Consider the string IIT Kharagpur ”. It is stored as: 0 13 I I T K h a r a g p u r \ 0 · · · 73 73 84 32 · · · 0 · · · The length of this string is 13 [0 · · · 13]. Lect 20 Goutam Biswas
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4 & $ % Character Array An 1-D array of type char is used to store a string . Lect 20 Goutam Biswas
Image of page 4
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5 & $ % Initialization of String #include <stdio.h> #define MAXLEN 100 int main() // stringInit.c { char a[MAXLEN]={’B’,’i’,’g’,’ ’, ’B’,’a’,’n’,’g’,’\0’}, b[MAXLEN]="Black Hole", c[]="Quantum Gravity", *cP="Super String" ; Lect 20 Goutam Biswas
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6 & $ % printf("a: %s\nb: %s\nc: %s\n*cP: %s\n", a, b, c, cP) ; printf("a[0] = %c, b[1] = %c, c[2] = %c, cP[3] a[0], b[1], c[2], cP[3]) ; return 0; } Lect 20 Goutam Biswas
Image of page 6
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7 & $ % Output $ cc -Wall stringInit.c $ a.out a: Big Bang b: Black Hole c: Quantum Gravity *cP: Super String a[0] = B, b[1] = l, c[2] = a, cP[3] = e Lect 20 Goutam Biswas
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8 & $ % Note All constant strings are stored in the read-only memory. Space is allocated for a[], b[], c[] to store the strings. But the pointer cP directly points to the begining of the string in the read-only memory. Any attempt to change that location results in segmentation violation in GCC. Lect 20 Goutam Biswas
Image of page 8
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 9 & $ % Initialization of String #include <stdio.h> #define MAXLEN 100 int main() // stringInit1.c { char a[MAXLEN]={’B’,’i’,’g’,’ ’, ’B’,’a’,’n’,’g’,’\0’}, b[MAXLEN]="Black Hole", c[]="Quantum Gravity", *cP="Super String" ; Lect 20 Goutam Biswas
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10 & $ % a[0] = ’A’, b[0] = ’A’, c[0] = ’A’ ; cP[0] = ’A’ ; // segmentation fault return 0; } Lect 20 Goutam Biswas
Image of page 10
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 11 & $ % Reading a String The library function scanf() can be used to read a string. The format conversion specifier for a sequence of non-white-space characters is %s .
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern