Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan

Similar presentations


Presentation on theme: "Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan"— Presentation transcript:

1 Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan

2 printf (“Average = %f”, avg);
1. introduction A string is a grouping of characters; i.e. words or phrases. We have already used strings constants extensively in printf and scanf statements. For example: printf (“Average = %f”, avg); The first argument “Average = %f” is a string constant. It consists of 12 characters. Note that the blanks are considered in the string length. Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 2

3 2. String constants A string constant can be associated with #define.
Example: #define INV_INPUT “Invalid Input Data” #define INSUFF_DATA “Insufficient Data” Dr. Soha S. Zaghloul updated by Rasha ALEidan

4 3. Strings declaration Consider the following statement
char string_var[30]; The previous statement declares a variable named string_var. Its type is char with length 30: i.e. a string consisting of 30 characters. Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 4

5 4. Strings initialization
Consider the following statement char str[20] = “Initial Value”; The previous statement initializes a string variable named str. The value given is “Initial Value”. Let us look to str in memory after this declaration and initialization: Note that the first letter is positioned at 0 and the last one at position 19. Position 13, \0, read as the null character marks the end of the string. I n i t a l V u e \0 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 5

6 4. Strings initialization (cont’d)
The null character is counted within the string length. It gives to a string the flexibility to have a variable length. Therefore, the minimum size of str is 1 (subscript 0). The maximum length is 20 (subscript 19). All C’s string-handling functions simply ignore whatever is stored in the cells following the null character \0. I n i t a l V u e \0 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 6

7 4. Strings initialization (cont’d)
The null character is counted within the string length. It gives to a string the flexibility to have a variable length. Therefore, the minimum size of str is 0. The maximum length is 20. All C’s string-handling functions simply ignore whatever is stored in the cells following the null character \0. N U M B E R S A D T I G \0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 7

8 printf (“Topics: %s \n”, str_var);
5. Strings with printf Use the conversion specifier %s to handle string variables in printf. Example: printf (“Topics: %s \n”, str_var); The characters of the string are printed until a terminating null character is encountered. Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 8

9 5. Strings with printf The default of the output is “right-justified”. Placing a minus sign causes the output to be “left-justified”. A number before s specifies the number of columns in which the string is to be displayed. Example: char writer[20] = “Ahmad Ragab”; //11 chars printf (“Mr. %-20s \n”, writer); Outputs: Mr.~Ahmad~Ragab~~~~~~~~~ Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 9

10 6. Strings with scanf Use %s to handle string variables in scanf.
Do not put the & for strings in scanf. (This will be justified later when you study arrays). Scanf() : Reads characters until the first whitespace character is encountered. It does not care how large the array is.( scanf() can write beyond the end of the array). Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 10

11 7. Strings with printf and scanf - Example
#include <stdio.h> #define STRING_LEN 10 int main (void) { char dept[STRING_LEN]; int course_num; char days[STRING_LEN]; int time; printf (“Enter department code, course number, “); printf (“days and time \n”); scanf (“%s%d%s%d”, dept, &course_num, days, &time); printf (“%s %d meets %s at %d\n”, dept, course_num, days, time); return (0); } // end of main function Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 11

12 8. Example Output Enter department code, course number, days and time CSC _ CSC 201 meets 135 at 11 printf (“Enter department code, course number, “); printf (“days and time \n”); scanf (“%s%d%s%d”, dept, &course_num, days, &time); printf (“%s %d meets %s at %d\n”, dept, course_num, days, time); When entering strings data using scanf, a white space specifies the location of the null character. In other words, internal spaces are not allowed within strings entered through the scanf. Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 12

13 9. String library functions
Apart from the string initialization, the assignment operator DOES not work with a string. Example: char string1[20] = “test string”; //this is correct char string1[20]; string1 = “test string”; //this is wrong C provides the string assignment operation through library functions. This is called string.h. Therefore, if your program uses C string library functions you should include string.h at the start: #include <string.h> Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 13

14 10. String assignment: strcpy
strcpy copies the second argument into its first one. The faulty line in the previous slide should be written as: char string1[20]; strcpy (string1, “test string”); //this is correct Consider this example: strcpy (string1, “a very long test string”); //overflow The result of the above example is unpredictable: It may overwrite other variables The system may generate a run-time error Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 14

15 10. String assignment: strncpy
strncpy takes an extra argument to avoid the unpredictable error that may be caused by strcpy. The extra argument is n: the number of characters to copy. If the source string is longer than n characters, only the first n characters are copies. Example: This will copy only the first 20 characters of the string constant. Therefore, string1 will be as follows in the memory: char string1[20]; strncpy (string1, “a very long test string”, 20); a v e r y l o n g t s 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 15

16 10. String assignment: strncpy (cont’d)
Note that the stored string is invalid because it does not contain the null character /0. In order to avoid this, n should be equal to (destination length – 1), which is 19 in this example. Note that string1[19] = ‘\0’ a v e r y l o n g t s 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 char string1[20]; strncpy (string1, “a very long test string”, 19); a v e r y l o n g t s \0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 16

17 10. Substring: strncpy Consider the following code segment:
char result1[10], s1[15] = “Sep. 18, 2014”; strncpy (result1, s1, 9); Copies 9 characters from s1 to result1 starting from s1[0] strncpy (result1, &s1[0], 9); S e p . 1 8 , 2 4 \0 ? 3 5 6 7 9 10 11 12 13 14 s1 S e p . 1 8 , \0 2 3 4 5 6 7 9 result1 char result2[10], s1[15] = “Sep. 18, 2014”; strncpy (result2, &s1[5], 2) Copies 2 characters from s1 to result2 starting from s1[5] 1 8 \0 ? 2 3 4 5 6 7 9 result2 Dr. Soha S. Zaghloul updated by Rasha ALEidan Dr. Soha S. Zaghloul 17


Download ppt "Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan"

Similar presentations


Ads by Google