Strings Md. Imran Hossain Showrov (showrovsworld@gmail.com) 17 1
Outline  What is a String?  Defining A String  What is NULL Character?  Initialization of a String  Reading and Writing a String
What is a String?  The array represents a list of elements of the same data type.  Most of the programming languages handle array of characters with a special provision for stings.  Strings are actually one-dimensional array of characters terminated by a null character (represent as '0‘).
What is a String?  null character is also named as string termination character. It is used to identify the length of the string  Initially, the string is nothing but an array of individual characters, and each individual element is accessible via subscripts.
Defining A String  A string definition may be expressed as storage-class char string-name [expression]; Example: char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'};
String Example1 char name[50]; /* name is a string that can consist of maximum 50 characters */ static char address[100]; /* address is a static string of maximum 100 characters */
String Example2 char greeting[] = "Hello"; Following is the memory presentation of the above defined string in C/C++  Actually, we do not place the null character at the end of a string constant.The C compiler automatically places the '0' at the end of the string when it initializes the array.
What is NULL Character  One of the most important issues in handling strings is null character.  Null character is used to terminate the string.  It is needed to compute the actual length of the string.  The string definition specifies the maximum length support. i.e. 50 in case of name[50] defines the maximum length of name.
What is NULL Character (cont..)  Example: char name [50] = “Jitender”;  In this example, the length of actual stored string is 8. In that case 9th element of the array i.e. name[8] location will be used to stored null character. J i t e n d e r ‘0’ …………… [0] [1] [2] [3] [4] [5] [6] [7] [8] [49]
What is NULL Character (cont..)  The null character is not counted towards the length of the string, but takes memory of one element.  Null character cannot be read or written, but is used internally only.
Initialization of a String  The string can be initialized as follows: - char str[30] = “Hello World”; char str[30] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘ ’,‘W’,‘o’,‘r’,‘l’,‘d’}; H e l l o W o r l d 0 ………….. str [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [29]
Reading and Writing a String  A string can be read and written by using a single scanf and printf function using %s (s stands for string) and no loop in needed, which was compulsory for reading and writing arrays of other types.
Reading and Writing a String (Example) #include<stdio.h> #define MAX_SIZE 50 main() { char str [MAX_SIZE]; printf(“Enter a string of maximum %d characters: ”, MAX_SIZE); scanf(“%s”, str); /* no ampersand (&) is used here*/ printf(“The entered string is %sn”, str); }  After the reading is complete, the null character is appended automatically at the next position inside the array.
Processing the String Functions are used to process with help of string.h header file or by processing all characters individually. 1. int strlen (string_var): to compute the length of the string, not counting null character. 2. strcpy (dst_string, src_string): to copy a source string into destination string 3. int strcmp(str1, str2): to compare str1 with str2 4. strcat(dst_string, src_string): to append copy of the source string at the end of destination string Here, dst_string = destination string src_string = source string
Lecture 17 - Strings

Lecture 17 - Strings

  • 1.
    Strings Md. Imran HossainShowrov (showrovsworld@gmail.com) 17 1
  • 2.
    Outline  What isa String?  Defining A String  What is NULL Character?  Initialization of a String  Reading and Writing a String
  • 3.
    What is aString?  The array represents a list of elements of the same data type.  Most of the programming languages handle array of characters with a special provision for stings.  Strings are actually one-dimensional array of characters terminated by a null character (represent as '0‘).
  • 4.
    What is aString?  null character is also named as string termination character. It is used to identify the length of the string  Initially, the string is nothing but an array of individual characters, and each individual element is accessible via subscripts.
  • 5.
    Defining A String A string definition may be expressed as storage-class char string-name [expression]; Example: char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'};
  • 6.
    String Example1 char name[50]; /*name is a string that can consist of maximum 50 characters */ static char address[100]; /* address is a static string of maximum 100 characters */
  • 7.
    String Example2 char greeting[]= "Hello"; Following is the memory presentation of the above defined string in C/C++  Actually, we do not place the null character at the end of a string constant.The C compiler automatically places the '0' at the end of the string when it initializes the array.
  • 8.
    What is NULLCharacter  One of the most important issues in handling strings is null character.  Null character is used to terminate the string.  It is needed to compute the actual length of the string.  The string definition specifies the maximum length support. i.e. 50 in case of name[50] defines the maximum length of name.
  • 9.
    What is NULLCharacter (cont..)  Example: char name [50] = “Jitender”;  In this example, the length of actual stored string is 8. In that case 9th element of the array i.e. name[8] location will be used to stored null character. J i t e n d e r ‘0’ …………… [0] [1] [2] [3] [4] [5] [6] [7] [8] [49]
  • 10.
    What is NULLCharacter (cont..)  The null character is not counted towards the length of the string, but takes memory of one element.  Null character cannot be read or written, but is used internally only.
  • 11.
    Initialization of aString  The string can be initialized as follows: - char str[30] = “Hello World”; char str[30] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘ ’,‘W’,‘o’,‘r’,‘l’,‘d’}; H e l l o W o r l d 0 ………….. str [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [29]
  • 12.
    Reading and Writinga String  A string can be read and written by using a single scanf and printf function using %s (s stands for string) and no loop in needed, which was compulsory for reading and writing arrays of other types.
  • 13.
    Reading and Writinga String (Example) #include<stdio.h> #define MAX_SIZE 50 main() { char str [MAX_SIZE]; printf(“Enter a string of maximum %d characters: ”, MAX_SIZE); scanf(“%s”, str); /* no ampersand (&) is used here*/ printf(“The entered string is %sn”, str); }  After the reading is complete, the null character is appended automatically at the next position inside the array.
  • 14.
    Processing the String Functionsare used to process with help of string.h header file or by processing all characters individually. 1. int strlen (string_var): to compute the length of the string, not counting null character. 2. strcpy (dst_string, src_string): to copy a source string into destination string 3. int strcmp(str1, str2): to compare str1 with str2 4. strcat(dst_string, src_string): to append copy of the source string at the end of destination string Here, dst_string = destination string src_string = source string