15 - ICS 103 Computer Programming in C Handout-15 Topic STRINGS Objective To know what is string and how to handle it To know how to read and print

15 - ICS 103 Computer Programming in C Handout-15 Topic...

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

ICS 103: Computer Programming in C Handout-15 Topic: STRINGS Objective: To know what is string and how to handle it? To know how to read and print Strings. To know about some important built-in string functions and how to use them? What is String? string  is any sequence of characters enclosed in double quotes . There is no separate data type for strings as  integer float  or  double . The string data type can be considered as a char array . The difference is that a character variable can  hold only one character but a string can have more than one character in a character array. For example, a string called  name  with  9  characters can be declared as:   char name [9] = “I like C” ; Check for the double quotes, the char array size  9 . Here the  name  can be considered as one string or as an array of  characters. That is if you refer  name  it is “ I like C ” and if you refer  name[0]  it is ‘ I ’,  name[1]  is   ‘ ‘ (a blank),  name[2]  is ‘ l ’,  name[3]  is ‘ i ’, and so on. The last character  name[8]  is  ‘\0’  which indicates a  NULL  character  which is not displayed  but is stored as last character of the string to indicate its end Note :   Strings are stored as arrays of characters and have a special       ‘\0’       termination character called       NULL     appended      (attached) to them to signify the end of the string     .   Note that if the number of characters including the   ‘\0’   is more than the size of the array the results will be  unpredictable. However, if the size of the array is more than the number of characters the extra spaces after the last  ‘\0’ character are kept blank and not referred because the string ends at  ‘\0’ . So, always make sure that the size of the array   is sufficient for the string.   For example, the above declaration would be wrong if we write char name [8] = “I like C” ;   The size can be ignored also. In that case the size is considered as that of the number of characters specified in the  declaration. char name [ ] = “I like C” ; String Input/Output: The easiest way to input strings is by using the C library function  gets  (means get string). The  gets( )  function reads a  string of characters entered at the keyboard until you strike the enter key (carriage return) . The carriage return does not  become part of the string; instead a null terminator  ‘\0’  is placed at the end.  For example , in the following program fragment Page 1 of 15
Image of page 1
char str[80] ;   gets (str) ; and if the user enters Rafiqul Zaman Khan   and presses the enter key the string variable  str
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 15 pages?

  • Spring '10
  • zaman
  • ASCII, Lower case, upper case, Rafiqul Zaman Khan

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

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes