Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.

Slides:



Advertisements
Similar presentations
Problem Solving & Program Design in C
Advertisements

Introduction to C Programming
Programming and Data Structure
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Chapter 10.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character.
Computer Science 210 Computer Organization Strings in C.
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
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.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 8 CHARACTER AND STRINGS
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
1 This chapter covers both string constants (or literals, as they're called in the C standard) and string variables, which can change during the execution.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
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.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
5.6 String Processing Part 2. Sprintf(destnvar,…..regularprintf) Write formatted data to string Same as printf except the output is put in variable. A.
String operations. About strings To handle strings more easily, we need to include a library> #include To see what the library allows us to do, look here:
Strings, Pointers and Tools
Strings, Slide Fundamental Programming Strings.
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
Today’s Material Strings Definition Representation Initialization
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
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.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Pointers. Pointer Arithmetic Since arrays consist of contiguous memory locations, we can increment (or decrement) the addresses to move through the array.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.
Lecture 8 String 1. Concept of strings String and pointers
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
CS111 Computer Programming
Chapter 8 Character Arrays and Strings
String manipulation string.h library
Exercise Arrays.
Presentation transcript:

Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples of character string constant are as follows. “C for Engineers” “Programming is fun” C f o r E n g i n e e r s \0 Null character

9.2 Declaring and Initializing Strings A character string in C is an array of type char. As with arrays of other data types, all character strings have to be declared. In addition to the name of the string, we have to tell the compiler how many characters there will be in the string so that the compiler can provide the necessary storage to hold the characters. There are two ways in which we can declare a character string: 1. Array notation 2. Pointer notation The Array Form When we declare a character string using array notation, we have to tell the compiler how many characters there will be in the string and also the name of the array.

char name [20], buffer [11]; This statement creates two arrays of type char. The array name[] consists of 20 elements, while the array buffer has 11 elements. Thus the array name[] can store a string of 19 characters, while the array buffer can store a string of 10 characters. | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Buffer [ ] buffer

9.2.2 The Pointer Form Character string can also be declared using pointers as in the statement. Char *planet = “jupiter”; The C compiler stores this as a string of eight characters. In addition, the C compiler will also creates a pointer to type char called planet. The pointer planet initially pointers to the first character of the string “jupiter”, that is, it contains the address in memory of the character ‘j’. However, planet is a variable and can be changed in the program. For example, we can use the increment operator as in ++ planet to change its value. Now, planet points to the second character in the string “jupiter”.

| j | u | p | i | t | e | r | \0 | planet planet++ | j | u | p | i | t | e | r| \0 | planet

9.3 String Input and Output Two steps are in reading a string in to a program. First, we must create a character array and set aside sufficient space to store the string. Second, we must use a string input function to read in the string. We create space for the string by including and explicit size in the array declaration. For example, the statement. Char buffer [81]; creates a character array buffer[ ] consisting of 81 elements. We can use the scanf() function with the %s format specifier to read in a string as in scanf(“%s”, buffer);

There is no address-of (&) operator preceding the name of the array. This is because the name of the array is also the address of the first element of the array The gets() Function The gets() library function (for get string) reads in a line of text from the standard input device. The following example reads in a string and prints a greeting. #include void main(void) { char name[81]; printf(“\n What is your name? “); gets(name); printf(“\n Greetings %s”, name); }

9.3.2 The puts () Function The puts() function (for put string) prints a string on the screen. It automatically adds a newline character to the end of the string. For example, the statement puts(“Greetings”); 9.4 Determining The Length of Strings The C library strlen() function returns the length of a string. The argument to the function is the address of a null terminated string. The function counts the number of characters in the string. The null character is not included in the count. We can also determine the number of characters in a string using the sizeof() operator. The sizeof operator includes the null character in its count.

Example: The strlen() Function The program reads several character strings and prints the string and the length of the string. It uses the strlen() function to determine the length of each string. #include void mani(void) { char buffer [ ] = “First String”; printf(“\n The length of the string is: %d”, strlen(buffer); } Program output The length of the string is: 12

9.5 Copying Strings The C library strcpy() function copies one string over another. It takes two arguments of type pointer to char. The two arguments are pointers that point to null terminated strings. The first string is the target string, and the second string is the source string for the copy operation. The function copies all characters in the source string (including the terminating null character) on to the target string. Thus the target string is overwritten, but the source string remains unchanged. The source string can be a string constant.

#include void main(void) { char target_str[50] = {“The first string”}; char *source_str = “Second string”; printf(“Before strcpy(): \n”); printf(“Target string = |%s|\n”, target_str); printf(“Source string = |%s|\n”, source_str); strcpy ( target_str, source_str ); printf(“After strcpy(): \n); printf(“Target string = |%s|\n”, target_str); printf(“Source string = |%s|\n”, source_str); }

Program output: Before strcpy(): Target string = |The first string| Source string = |Second string| After strcpy(); Target string = |Second string| Source string = |Second string|

9.6 Concatenating Strings The strcat() function adds or concatenates two strings str1 and str2 to form a single string. The result is stored in str1. The function removes the null character at the end of the first string and adds new null character at the end of the concatenated string. Thus str1 is changed, but str2 remains unchanged. Example: String Concatenation problem statement: write a program that combines two strings to form a single string.

#include void main(void) { char str1[11] = “12345”; char *str2 = “67890”; printf(“Before strcat(): \n”); printf(“Target string = |%s|\n”, str1); printf(“Source string = |%s|\n”, str2); strcat (str1, str2 ); printf(“After strcat(): \n); printf(“Target string = |%s|\n”, str1); printf(“Source string = |%s|\n”, str2); }

Program output: Before strcat(): Target string = |12345| Source string = |67890| After strcat(); Target string = | | Source string = |67890|

9.7 Comparing Strings Since strings are represented by character array in C, we cannot compare two strings using the relational operators such as ==, !=, and >=. Thus, if str1 and str2 are two strings, then we cannot use the following statement. If (str1 == str2)/* Wrong!!*/ to determine if the two strings are equal. The C library provides a function called strcmp() for comparing the contents of two strings. The strcmp() function accepts two pointers as arguments and returns the following values: 0if the two string are equal > 0if the first string is larger < 0if the first string is smaller

Any Questions