CHAPTER 8 CHARACTER AND 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

Lecture 9: Character and String
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.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
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.
CHAPTER 8: Characters Arrays (Strings) CSEB113 PRINCIPLES of PROGRAMMING by Badariah Solemon 1BS (May 2013)
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Chapter 9 Character Strings
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 10.
Chapter 8 Characters and Strings 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 Fundamentals of Strings and Characters Characters.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
1 Character Processing How characters can be treated as small integers? How characters are stored and manipulated in a machine? How use is made of certain.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
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.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
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’};
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Characters, Strings, And The string Class Chapter 10.
Chapter 8: Character and String CMPD144: Programming 1.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
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:
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
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.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Chapter Characters, Strings, and the string class 10.
CTYPE.H Introduction  The ctype header is used for testing and converting characters.  A control character refers to a character that.
Characters and Strings
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
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.
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.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Character Processing How characters can be treated as small integers?
Chapter 8 - Characters and Strings
10.1 Character Testing.
Character Processing How characters can be treated as small integers?
C Characters and Strings
Presentation transcript:

CHAPTER 8 CHARACTER AND STRINGS Department of Systems and Networking Ver.1 Systems and Networking

FUNDAMENTAL OF STRINGS AND CHARACTERS Characters are the fundamental building blocks of source programs. A program may contain character constants. Character constant is an integer value represented as a character in a single quotes. Example : ‘a’ represents the integer value of a, which is 97 (based on ASCII value), while ‘A’ represents the integer value of A, which is 65. Ver.1 Systems and Networking

Systems and Networking Character Data Characters in C consist of any printable or nonprintable character in the computer’s character set including lowercase letters, uppercase letters, decimal digits, special characters and escape sequences Each character is represented as an integer or as one byte of memory. The integer value stored is depends on the character set used by the computer. Two common used character sets are ASCII (American Standard Code for Information Interchange- primary used in micro/mini or mainframes computer) and EBCDIC (Extended Binary Coded Decimal Interchange Code-IBM mainframes). Ver.1 Systems and Networking

Systems and Networking ASCII Character set There are 128 different characters in ASCII. Each character is represented by a positive integer, ranging from 0 to 127. Required 7 bits to represents the integer value, while the left-most bit is used for the sign. Ver.1 Systems and Networking

Systems and Networking ASCII Table (0-127) Reference: http://www.plcdev.com/ascii_chart Ver.1 Systems and Networking

Systems and Networking Example #include <stdio.h> int main(void) { char my_A = 'A'; char my_Z = 'Z'; char my_a = 'a'; char my_z = 'z'; printf("\nASCII value for A is %d", my_A); printf("\nASCII value for Z is %d",my_Z); printf("\nASCII value for a is %d", my_a); printf("\nASCII value for z is %d",my_z); printf("\n"); printf("\n65 in ASCII represents %c",65); printf("\n90 in ASCII represents %c",90); printf("\n97 in ASCII represents %c",97); printf("\n122 in ASCII represents %c",122); return(0); } Output ASCII value for A is 65 ASCII value for Z is 90 ASCII value for a is 97 ASCII value for z is 122 65 in ASCII represents A 90 in ASCII represents Z 97 in ASCII represents a 122 in ASCII represents z Press any key to continue . . . Ver.1 Systems and Networking

Input and output of Character Data In addition to printf and scanf for output and input of character data, standard C has two other functions for input and output. getchar – to input the character data putchar – to display the output of the character data. Both are declared in the header file stdio.h Ver.1 Systems and Networking

Systems and Networking Example using scanf or getchar to input a character for a variable type char: char ch; scanf (“%c”,&c); or ch=getchar(); Function getchar has no parameter. It returns an integer that is counterpart of the character read in the computer’s character system and stores it in the variable ch. Ver.1 Systems and Networking

Systems and Networking Example using printf and putchar to display the character data. char ch; printf (%c”,ch); or putchar(ch); Function putchar requires one integer parameter. It converts the integer to a character and prints it on the standard output device. Ver.1 Systems and Networking

Character-Handling Library Function The character-handling function perform useful tests and manipulations of character data. These functions are declared in the standard header file ctype.h #include <ctype.h> Each function receives a character, represented as an int or EOF as an argument. Ver.1 Systems and Networking

Summary on Character Handling Library Function prototype Function description int isdigit (int c); Returns a true value if c is a digit and 0 (false) otherwise. int isalpha(int c); Returns a true value if c is a letter and 0 otherwise. int isalnum (int c) ; Returns a true value if c is a digit or a letter and 0 otherwise. int isxdigit(int c); Returns a true value if c is a hexadecimal digit character and 0 otherwise. int islower (int c); Returns a true value is c is a lowercase letter and 0 otherwise. int isupper(int c); Returns a true value is c is a uppercase letter and 0 otherwise. int tolower(int c); If c is an uppercase letter, tolower returns c as a lowercase letter, otherwise it will returns the argument unchanged. int toupper (int c); If c is a lowercase letter, toupper returns c as an uppercase letter, otherwise it will returns the argument unchanged. Ver.1 Systems and Networking

Systems and Networking continue Function prototype Function description int isspace(int c); Returns a true value if c is a white-space character-newline (‘\n’), space(‘ ‘), form feed(‘\f’),carriage return (‘\r’), horizontal tab (‘\t’) or vertical tab (‘\v’)- and 0 otherwise. int iscntrl(int c); Returns a true value if c is a control character and 0 otherwise. int ispunct(int c); Returns a true value if c is a printing character other than a space, a digit or a letter and returns 0 otherwise. int isprint(int c); Returns a true value if c is a printing character including a space (‘ ‘) and returns 0 otherwise. int isgraph(int c); Returns a true value if c is a printing character other than a space (‘ ‘) and returns 0 otherwise. Ver.1 Systems and Networking

Systems and Networking String String is a series of characters treated as a single unit. A string constant is a sequence of characters in a source program. It is enclosed by double quotation marks (“ “). A string in C is implemented as an array. Declaring a string: char str[10]; This means the variable char str will an array of ten characters. Initialize the string variable as below: char str[10] =“New Year”; Ver.1 Systems and Networking

String Initializations index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] N e w Y a r \o data Variable str in memory after the declaration with initialization. At position 8 (index), the variable str contains the character ‘\o’- the null character. This marks the end of a string. Ver.1 Systems and Networking

String Initializations Initialization of string during compile time. Example 1 char str[10]=“Good”; Example 2 char str[10]={‘G’, ’o’, ‘o’, ‘d’}; This example is using the notation for array initialization. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] G o d \o index data Ver.1 Systems and Networking

String Initializations String also can be initialize without declaring the array size. Example 3 char str[] =“Good”; This will create an array with 5 elements to store the four characters and the terminating character. [0] [1] [2] [3] [4] G o d \o Ver.1 Systems and Networking

Output of string variables There are two functions to be used: printf puts char str[10]=“Good”; Example 1: printf(“%s”,str); Example 2: puts(str); Good Good Ver.1 Systems and Networking

Systems and Networking continue #include <stdio.h> int main() { char str[10]="Good"; printf("%s\n\n",str); puts(str); return 0; } Good Press any key to continue . . . Ver.1 Systems and Networking

Systems and Networking Ver.1 Systems and Networking

Systems and Networking Ver.1 Systems and Networking

Input of string variables Two functions: scanf gets char str[10]; Example 1: scanf(“%s”,str); scanf skips all whitespace characters preceding the string constant type using keyboard. Hence it is not possible to input a string value containing whitespace. Example : char str[10]; scanf(“%s”,str); user input  Good day and press Enter, any whitespace characters preceding the first word, Good will be skipped. Ver.1 Systems and Networking

Systems and Networking continue [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] G o d \o The remaining word, day with the new-line character ‘\n’ remain in the input stream to be used by the next input statement, if any in the program. Example 2: gets(str); char str[10]; gets(str); gets does not skip whitespace character. It reads characters into its parameter from the standard input device until a new-line or end-of-file character is encountered. It will returns the string that it has just read. Ver.1 Systems and Networking

Systems and Networking continue char str[10]; gets(str); User input =Good day output [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] G o d a y \o Ver.1 Systems and Networking

Systems and Networking continue Please enter your name asmidar Please enter your age 23 Your name is asmidar and your age is 23 Press any key to continue . . #include <stdio.h> int main() { char name[10]; int age; printf("Please enter your name \n"); scanf("%s",name); printf("Please enter your age\n"); scanf("%d",&age); printf("Your name is %s and your age is %d",name,age); return 0; } 1 User input User input Please enter your name ali ahmad Please enter your age Your name is ali and your age is -858993460Press any key to continue … 2 User input ???? Why output 2 display the age as random numbers? Why we cannot input the age? Ver.1 Systems and Networking

Systems and Networking Ver.1 Systems and Networking

Systems and Networking continue Ver.1 Systems and Networking

Systems and Networking continue #include <stdio.h> int main() { char name[10]; int age; printf("Please enter your name \n"); gets(name);//gets name with whitespace printf("Please enter your age\n"); scanf("%d",&age);//use scanf for integer data puts(name);//display name with whitespace printf(" age is %d",age); return 0; } Please enter your name asmidar abu bakar Please enter your age 23 age is 23Press any key to continue … Ver.1 Systems and Networking

Systems and Networking Ver.1 Systems and Networking

Systems and Networking String processing To use string manipulation functions, the functions need to be declared in #include <string.h> The fundamental string operations include the following: Copying string Comparing strings Computing lengths of string Concatenating strings Searching strings for substrings Tokenizing string Ver.1 Systems and Networking

Systems and Networking Copying strings strcpy – to copy one string into another. This function has two string parameters. It copies the second parameter into the first one and returns the first parameter. Example : char str1[20],str2[20]; let str1 = “C is fun”; Assume we have the statement :strcpy(str2,str1); This statement will copy the value of str1 to str2. The value in str2 now will be ‘C is fun”. The value in str1 remain unchanged. Ver.1 Systems and Networking

Systems and Networking Complete example #include <stdio.h> #include <string.h> int main() { char str1[20]="I love C"; char str2[20]; puts(str1); strcpy(str2,str1); printf(" The value of string 2 is %s\n",str2); return 0; } I love C The value of string 2 is I love C Press any key to continue . . . Ver.1 Systems and Networking

Systems and Networking Ver.1 Systems and Networking

Systems and Networking Example 2 #include <stdio.h> #include <string.h> int main() { char str1[20]="I love C"; char str2[20]=" C is fun to learn"; puts(str1); strcpy(str2,str1); printf(" The value of string 2 is %s\n",str2); return 0; } I love C The value of string 2 is I love C Press any key to continue . . . Why the output is like this??? Ver.1 Systems and Networking

Systems and Networking Ver.1 Systems and Networking

Systems and Networking Comparing strings strcmp is used to compare two strings. It has two parameters of type string. It returns an integer that is less than 0, equal to 0 or greater than 0, if the first string parameter is less than, equal to or greater than the second string parameter. How the comparing is taken place? Computer will look at the first characters in each strings. If the first characters match, it continues with the next characters until the two characters being compared do not match. If all characters in both strings match and the two strings are the same length, there are considered equal. Ver.1 Systems and Networking

Systems and Networking continue Example: based on ASCII computer The string “data” is less than the string “date” d a t d a t e 1 2 3 4 ASCII value : d = 100, a=97, t=116, e=101 Both characters on strings are same except at position 4, where data is ended with ‘a’, while date is ended with ‘e’. The value for ‘e’ is bigger than ‘a’, hence it is correct that “data” is less than “date”. Ver.1 Systems and Networking

Systems and Networking continue The string “A125” is greater than “15A” “data” is equal to “data” “data” is greater than “ data” “125” is greater than “123” “data” is less than “data “ Ver.1 Systems and Networking

Systems and Networking Example #include <stdio.h> #include <string.h> int main() { char str1[20]="data"; char str2[20]="date"; if(strcmp(str1,str2)> 0 ) printf("%s is greater than %s",str1,str2); else printf("%s is not greater than %s",str1,str2); return 0; } The function call strcmp(str1,str2) returns a negative integer, therefore the output produce is as below: data is not greater than datePress any key to continue . Ver.1 Systems and Networking

Systems and Networking #include <stdio.h> #include <string.h> int main() { char str1[20]="data"; char str2[20]="date"; if(strcmp(str2,str1)> 0 ) printf("%s is greater than %s",str2,str1); else printf("%s is not greater than %s",str2,str1); return 0; } Swap the string date is greater than dataPress any key to continue . . . Ver.1 Systems and Networking