Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Advertisements

Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
Nested LOOPS.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
 Integers and Characters  Character Strings  Input and Output.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
Repetition statements
ARRAYS.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Strings CSCI 112: Programming in C.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
ECE Application Programming
Strings (Continued) Chapter 13
Characters and Strings
C Characters and Strings
Fundamentals of Characters and Strings
© 2016 Pearson Education, Ltd. All rights reserved.
Lecture 8 String 1. Concept of strings String and pointers
ICS103 Programming in C Lecture 3: Introduction to C (2)
A First Book of ANSI C Fourth Edition
Strings A string is a sequence of characters treated as a group
Arrays in C.
Programming in C Input / Output.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Stuff CS 2308.
INPUT & OUTPUT scanf & printf.
Dale Roberts, Lecturer IUPUI
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Engr 0012 (04-1) LecNotes
INC 161 , CPE 100 Computer Programming
Week 9 – Lesson 1 Arrays – Character Strings
Functions.
1) C program development 2) Selection structure
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 9 - Formatted Input/Output
Arrays Strings and Parameter Passing CSCI N305
Lecture 11 Strings.
String What it is Why it’s useful
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length.
Chapter 8 Character Arrays and Strings
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
EECE.2160 ECE Application Programming
Exercise Arrays.
Functions Extra Examples.
Arrays.
C++ Programming Lecture 20 Strings
Strings #include <stdio.h>
Variables in C Topics Naming Variables Declaring Variables
Introduction to Problem Solving and Programming
Presentation transcript:

Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan

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

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

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

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

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

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

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

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

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

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

8. Example Output Enter department code, course number, days and time CSC 201 135 11 _ 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

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

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

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

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

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