Chapter 10 Character Strings

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Introduction to C Programming
ARDUINO CLUB Session 1: C & An Introduction to Linux.
Lectures 10 & 11.
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.
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.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Chapter 10.
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Chapter 10 - Character Strings. Array of Characters char word[] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’ }; word[0] 'H' word[1] 'e' word[2] 'l' word[3] 'l' word[4]
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
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.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Input & Output: Console
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Input, Output, and Processing
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
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.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
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.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
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.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2 - Introduction to C Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
A First Book of ANSI C Fourth Edition
Chapter 2 - Introduction to C Programming
Arrays in C.
Chapter 2 - Introduction to C Programming
C Arrays.
Introduction to C++ Programming
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Chapter 10 Character Strings CCSA 221 Programming in C Chapter 10 Character Strings AlHanouf AlAmr

Outline Arrays of Characters Variable-Length Character Strings Initializing and Displaying Character Strings Testing Two Character Strings for Equality Inputting Character Strings Single-Character Input The Null String Escape Characters More on Constant Strings Character Strings, Structures, and Arrays A Better Search Method Character Operations

What is a character string? In the statement: printf ("Programming in C is fun.\n"); the argument that is passed to the printf function is the character string "Programming in C is fun.\n“ The double quotation marks are used to delimit the character string, which can contain any combinations of letters, numbers, or special characters. When introduced to the data type char , you learned that a variable that is declared to be of this type can contain only a single character. To assign a single character to such a variable, the character is enclosed within a pair of single quotation marks. Thus, the assignment plusSign = '+'; has the effect of assigning the character '+' to the variable plusSign.

Arrays of Characters If you want to be able to deal with variables that can hold more than a single character, you can use an array of characters. You can define an array of characters called word as follows: char word [] = { 'H', 'e', 'l', 'l', 'o', '!' }; To print out the contents of the array word, you run through each element in the array and display it using the %c format characters. To do processings of the word (copy, concatenate two words, etc) you need to have the actual length of the character array in a separate variable !

Variable-Length Character Strings A method for dealing with character arrays without knowing the actual length of the character array: Placing a special character at the end of every character string. In this manner, the function can then determine for itself when it has reached the end of a character string after it encounters this special character. In the C language, the special character that is used to signal the end of a string is known as the null character and is written as '\0'. char word [] = { 'H', 'e', 'l', 'l', 'o', '!', '\0' }; How these variable-length character strings are used? Using a function that counts the number of characters in a character string.

Variable-Length Character Strings // Function to count the number of characters in a string #include <stdio.h> int stringLength (const char string[]){ int count = 0; while ( string[count] != '\0' ) ++count; return count; } int main (void) { const char word1[] = { 'a', 's', 't', 'e', 'r', '\0' }; const char word2[] = { 'a', 't', '\0' }; const char word3[] = { 'a', 'w', 'e', '\0' }; printf ("%i %i %i\n", stringLength (word1), stringLength (word2), stringLength (word3)); return 0; The function declares its argument as a const array of characters because it is not making any changes to the array Output: 5 2 3

Initializing and Displaying Character Strings Initializing a string: char word[] = "Hello!"; Is equivalent to: char word[] = { 'H', 'e', 'l', 'l', 'o', '!', '\0' }; As you can see, the size of the array is not specified in the previous 2 examples. We can specify the size of the string as follows: char word[7] = { "Hello!" }; In this example, the compiler has enough room in the array to place the terminating null character. However, in: char word[6] = { "Hello!" }; the compiler can’t fit a terminating null character at the end of the array, and so it doesn’t put one there.

Initializing and Displaying Character Strings In general, character-string constants in the C language are automatically terminated by the null character wherever they appear in your program. In this example: printf ("Programming in C is fun\n"); Function printf can determine when the end of a character string has been reached when it finds the null character that is automatically placed after the newline character. . To display an array of characters, we use the special format characters %s. e.g. printf ("%s\n", word);

String processing-Concatenate strings #include <stdio.h> // Function to concatenate two character strings void concat (char result[], const char str1[], const char str2[]) { int i, j; // copy str1 to result for ( i = 0; str1[i] != '\0'; ++i ) result[i] = str1[i]; // copy str2 to result for ( j = 0; str2[j] != '\0'; ++j ) result[i + j] = str2[j]; // Terminate the concatenated string with a null character result [i + j] = '\0'; } int main (void) const char s1[] = "Test " ; const char s2[] = "works." ; char s3[20]; concat (s3, s1, s2); printf ("%s\n", s3); return 0; Output: Test works.

Testing Two Character Strings for Equality You cannot directly test two strings to see if they are equal with a statement such as if ( string1 == string2 ) We can develop a function that can be used to compare two character strings as follows: bool equalStrings (const char s1[], const char s2[]) { int i = 0; bool areEqual; while ( s1[i] == s2 [i] && s1[i] != '\0' && s2[i] != '\0' ) ++i; if ( s1[i] == '\0' && s2[i] == '\0' ) areEqual = true; else areEqual = false; return areEqual; }

The function that we declared in the previous slide // Program to Test Strings for Equality #include <stdio.h> /***** Insert equalStrings function here *****/ int main (void) { const char stra[] = "string compare test"; const char strb[] = "string"; printf ("%i\n", equalStrings (stra, strb)); printf ("%i\n", equalStrings (stra, stra)); printf ("%i\n", equalStrings (strb, "string")); return 0; } The function that we declared in the previous slide Comparing same string Comparing same string Output: 1

Inputting Character Strings The scanf function can be used with the %s format characters to read in a string of characters up to a blank space, tab character, or the end of the line, whichever occurs first. char string[81]; scanf ("%s", string); Note that unlike previous scanf calls, in the case of reading strings, the & is not placed before the array name ! If the following characters are entered: Shawshank the string "Shawshank" is read in by the scanf function and is stored inside the string array. If the following line of text is typed instead: iTunes playlist just the string "iTunes" is stored inside the string array because the blank space. If the scanf call is executed again, this time the string "playlist" is stored inside the string array because the scanf function always continues scanning from the most recent character that was read in. & is not placed

Inputting Character Strings If s1 , s2 , and s3 are defined to be character arrays of appropriate sizes, execution of the statement: scanf ("%s%s%s", s1, s2, s3); with the input line of text: micro computer system results in the assignment of the string "micro" to s1 , "computer" to s2 , and "system“ to s3 . If the following line of text is typed instead: system expansion it results in the assignment of the string "system" to s1 , and "expansion" to s2.

// Program to illustrate the %s scanf format characters #include <stdio.h> int main (void) { char s1[81], s2[81], s3[81]; printf ("Enter text:\n"); scanf ("%s%s%s", s1, s2, s3); printf ("\ns1 = %s\ns2 = %s\ns3 = %s\n", s1, s2, s3); return 0; } If you type in more than 80 consecutive characters to the preceding program without pressing the spacebar, the tab key, or the Enter (or Return) key, scanf overflows the character array ! scanf has no way of knowing how large its character array parrameters are ! When handed a %s format, it simply continues to read and store characters until one of the noted terminator characters is reached. If you place a number after the % in the scanf format string, this tells scanf the maximum number of characters to read. Correct use of scanf for reading strings: scanf ("%80s", string); Output: Enter text: system expansion bus s1 = system s2 = expansion s3 = bus

Single-Character Input #include <stdio.h> // Function to read a line of text from the terminal void readLine (char buffer[]){ char character; int i = 0; do { character = getchar (); buffer[i] = character; ++i; } while ( character != '\n' ); buffer[i - 1] = '\0'; int main (void){ int i; char line[81]; for ( i = 0; i < 3; ++i ){ readLine (line); printf ("%s\n\n", line); return 0; Reading 1 line by repeated calls to the getchar function return successive single characters from the input. When the end of the line is reached, the function returns the newline character '\n' . Placing null at the end of every line Output: This is a sample line of text. abcdefghijklmnopqrstuvwxyz Abcdefghijklmnopqrstuvwxyz runtime library routines Read 3 lines of text

The Null String The null string: A character string that contains no characters other than the null character. The null string length is zero. In any program that asked the user to enter a text, if the user presses Enter key without entering any character before, the string that has been read is a null string. Examples: char empty[]= ""; char buf[100]= "";

//Program to count words in a piece of text #include <stdio.h> #include <stdbool.h> /***** Insert readLine function here *****/ // Function to determine if a character is alphabetic bool alphabetic (const char c){ if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ) return true; else return false;} // Function to count the number of words in a string int countWords (const char string[]){ int i, wordCount = 0; bool lookingForWord = true; for ( i = 0; string[i] != '\0'; ++i ) if ( alphabetic(string[i]) ){ if ( lookingForWord ){ ++wordCount; lookingForWord = false; } lookingForWord = true; return wordCount;} Function readLine Slide 15

bool endOfText = false; printf ("Type in your text.\n"); int main (void) { char text[81]; int totalWords = 0; bool endOfText = false; printf ("Type in your text.\n"); printf ("When you are done, press ‘Enter'.\n\n"); while ( ! endOfText ) { readLine (text); if ( text[0] == '\0' ) endOfText = true; else totalWords += countWords (text); } printf ("\nThere are %i words in the above text.\n", totalWords); return 0; Function readLine Slide 15 tests the input line to see if just the Enter was pressed then it is a null string.

Escape Characters The backslash character has a special significance that extends beyond its use in forming the newline ( \n )and null ( \0 ) characters. Other characters can be combined with the backslash character to perform special functions. These are referred to as escape characters. \a Audible alert \b Backspace \f Form feed \n Newline \r Carriage return \t Horizontal tab \v Vertical tab \\ Backslash \" Double quotation mark \' Single quotation mark \? Question mark

Escape Characters Examples: printf ("\aSYSTEM SHUT DOWN IN 5 MINUTES!!\n"); sounds an alert and displays the indicated message. printf ("%i\t%i\t%i\n", a, b, c); displays the value of a , spaces over to the next tab setting, displays the value of b, spaces over to the next tab setting, and then displays the value of c. printf ("\\t is the horizontal tab character.\n"); displays the following: \t is the horizontal tab character. Note that because the \\ is encountered first in the string, a tab is not displayed in this case. printf ("\"Hello,\" he said.\n"); results in the display of the message: "Hello," he said. c = '\''; assigns a single quotation character to c .

More on Constant Strings Without the line continuation character, your C compiler generates an error message if you attempt to initialize a character string across multiple lines; for example: char letters[] = { "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ" }; By placing a backslash character at the end of each line to be continued, a character string constant can be written over multiple lines: { "abcdefghijklmnopqrstuvwxyz\

More on Constant Strings Another way to break up long character strings is to divide them into two or more adjacent strings. Adjacent strings are constant strings separated by zero or more spaces, tabs, or newlines. The compiler automatically concatenates adjacent strings together. So, the letters array can also be set to the letters of the alphabet by writing char letters[] = { "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" }; The three printf calls: printf ("Programming in C is fun\n"); printf ("Programming" " in C is fun\n"); printf ("Programming" " in C" " is fun\n"); all pass a single argument to printf because the compiler concatenates the strings together in the second and third calls.

Character Strings, Structures, and Arrays Suppose we want to write a computer program that acts like a dictionary. we could type the word into the program, and the program could then automatically “look up” the word inside the dictionary and tell us its definition. Because the word and its definition are logically related, we can represent the word and its definition inside the computer as a structure. we can define a structure called entry to hold the word and its definition: struct entry { char word[15]; char definition[50]; }; The following is an example of a variable defined to be of type struct entry that is initialized to contain the word “blob” and its definition. struct entry word1 = { "blob", "an amorphous mass" }; Because we want to provide for many words inside our dictionary, it seems logical to define an array of entry structures, such as in struct entry dictionary[100];

Function equalStrings Slide 10 // Program to use the dictionary lookup program #include <stdio.h> #include <stdbool.h> struct entry { char word[15]; char definition[50]; }; /***** Insert equalStrings function here *****/ // function to look up a word inside a dictionary int lookup (const struct entry dictionary[], const char search[], const int entries) int i; for ( i = 0; i < entries; ++i ) if ( equalStrings (search, dictionary[i].word) ) return i; return -1; } Function equalStrings Slide 10

const struct entry dictionary[100] ={ int main (void){ const struct entry dictionary[100] ={ { "aardvark", "a burrowing African mammal" }, { "abyss", "a bottomless pit" }, { "acumen", "mentally sharp; keen" }, { "addle", "to become confused" }, { "aerie", "a high nest" }, { "affix", "to append; attach" }, { "agar", "a jelly made from seaweed" }, { "ahoy", "a nautical call of greeting" }, { "aigrette", "an ornamental cluster of feathers" }, { "ajar", "partially opened" } }; char word[10]; int entries = 10; int entry; printf ("Enter word: "); scanf ("%s", word); entry = lookup (dictionary, word, entries); if ( entry != -1 ) printf ("%s\n", dictionary[entry].definition); else printf ("Sorry, the word %s is not in my dictionary.\n", word); return 0; } Output: Enter word: agar a jelly made from seaweed Output (Rerun): Enter word: accede Sorry, the word accede is not in my dictionary.

A Better Search Method The method used by the lookup function is perfectly fine for a small-sized dictionary. If you start dealing with large dictionaries containing hundreds or perhaps even thousands of entries, this approach might no longer be sufficient because of the time it takes to sequentially search through all of the entries. What you are really looking for is an algorithm that reduces the search time in most cases, not just in one particular case. Such an algorithm exists under the name of the binary search. Binary Search Algorithm Search x in SORTED array M of size n. Step 1: Set low to 0, high to n – 1. Step 2: If low > high, x does not exist in M and the algorithm terminates. Step 3: Set mid to (low + high) / 2. Step 4: If M[mid] < x, set low to mid + 1 and go to step 2. Step 5: If M[mid] > x, set high to mid – 1 and go to step 2. Step 6: M[mid] equals x and the algorithm terminates.

// Dictionary lookup program using binary search #include <stdio.h> struct entry { char word[15]; char definition[50]; }; // Function to compare two character strings int compareStrings (const char s1[], const char s2[]) { int i = 0, answer; while ( s1[i] == s2[i] && s1[i] != '\0'&& s2[i] != '\0' ) ++i; if ( s1[i] < s2[i] ) answer = -1; /* s1 < s2 */ else if ( s1[i] == s2[i] ) answer = 0; /* s1 == s2 */ else answer = 1; /* s1 > s2 */ return answer; }

// Function to look up a word inside a dictionary int lookup (const struct entry dictionary[], const char search[], const int entries) { int low = 0; int high = entries - 1; int mid, result; while ( low <= high ) mid = (low + high) / 2; result = compareStrings (dictionary[mid].word, search); if ( result == -1 ) low = mid + 1; else if ( result == 1 ) high = mid - 1; else return mid; /* found it */ } return -1; /* not found */

const struct entry dictionary[100] ={ int main (void){ const struct entry dictionary[100] ={ { "aardvark", "a burrowing African mammal" }, { "abyss", "a bottomless pit" }, { "acumen", "mentally sharp; keen" }, { "addle", "to become confused" }, { "aerie", "a high nest" }, { "affix", "to append; attach" }, { "agar", "a jelly made from seaweed" }, { "ahoy", "a nautical call of greeting" }, { "aigrette", "an ornamental cluster of feathers" }, { "ajar", "partially opened" } }; int entries = 10; char word[15]; int entry; printf ("Enter word: "); scanf ("%s", word); entry = lookup (dictionary, word, entries); if ( entry != -1 ) printf ("%s\n", dictionary[entry].definition); else printf ("Sorry, the word %s is not in my dictionary.\n", word); return 0; } Output: Enter word: aigrette an ornamental cluster of feathers Output (Rerun): Enter word: acerb Sorry, that word is not in my dictionary.

Character Operations Character variables and constants are frequently used in relational and arithmetic expressions. Whenever a character constant or variable is used in an expression in C, it is automatically converted to an integer value. If (c >= 'a' && c <= 'z‘) The first part of this expression is actually comparing the value of c against the internal numerical representation of the character 'a‘. In ASCII, the character 'a' has the value 97 , the character 'b' has the value 98, and so on. printf ("%i \n", 'a'); displays 97. c = 'a' + 1; printf ("%c \n", c); Because the value of 'a' is 97 in ASCII, c = 97+ 1 = 98. The value 98 represent 'b' in ASCII, so 'b' will be displayed by the printf call. i = c - '0'; The character '0' is not the same as the integer 0, the character '0' has the numerical value 48 in ASCII. Therefore, if you suppose that c contained the character '5' , which, in ASCII, is the number 53 so I = 53 – 48 = 5. which results in the integer value 5 being assigned to i .

// Function to convert a string to an integer #include <stdio.h> int strToInt (const char string[]) { int i, intValue, result = 0; for ( i = 0; string[i] != ‘\0'; ++i ) if ((string[i] >= '0') && (string[i] <= '9')) intValue = string[i] - '0'; result = result * 10 + intValue; } return result; int main (void) printf ( "%i\n", strToInt("245") ); printf ( "%i\n", strToInt("100") + 25 ); printf ( "%i\n", strToInt("13x5") ); return 0; Output: 245 125 13