Principles of Programming - NI2005 1 Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Lecture 9: Character and String
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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.
 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)
 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.
Computer Science 210 Computer Organization Strings in C.
PRESENTED BY: ER. SUMANPREET KAUR LECTURER IN CE DEPTT. GPCG ASR.
 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.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
Introduction to C programming
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 8 CHARACTER AND STRINGS
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CS 108 Computing Fundamentals March 26, Class Notes Last day to withdraw from a class is Monday, April 6 Next week I sent s to a small number.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
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’};
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Chapter 8: Character and String CMPD144: Programming 1.
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:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
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. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
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).
UniMAP SEM I - 10/11EKT 120 Computer Programming1 Lecture 8 – Arrays (2) and Strings.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
Characters and Strings
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
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.
1 Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
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.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Fundamentals of Characters and Strings
CS 108 Computing Fundamentals November 2, 2017.
Strings A string is a sequence of characters treated as a group
Chapter 8 - Characters and Strings
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C++ Programming Lecture 20 Strings
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C Characters and Strings
Presentation transcript:

Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference between an integer digit and a character digit Character handling library String conversion functions Standard input/output library functions String manipulation functions

Principles of Programming - NI Fundamentals of Characters and Strings 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. A character is usually stored in the computer as an 8-bits (1 byte) integer. The integer value stored for a character depends on the character set used by the computer on which the program is running.

Principles of Programming - NI There are two commonly used character sets: ASCII (American Standard Code for Information Interchange) EBCDIC (Extended Binary Coded Decimal Interchange Code) Fundamentals of Characters and Strings

Principles of Programming - NI char num = 1 and char num = ‘1’ are not the same. char num = 1 is represented in the computer as char num = ‘1’ on the other hand is number 49 according to the ASCII character set. Therefore, it is represented in the computer as Difference Between an Integer Digit and a Character Digit

Principles of Programming - NI Example: ASCII character #include void 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); }

Principles of Programming - NI Sample output ASCII value for A is 65 ASCII value for Z is 90 ASCII value for a is 97 ASCII value for z is in ASCII represents A 90 in ASCII represents Z 97 in ASCII represents a 122 in ASCII represents z

Principles of Programming - NI Example cont… #include void main(void) { char ch; printf("enter a character: "); scanf("%c", &ch); if (ch >= 'A' && ch <= 'Z') { printf("\ncapital letter\n"); } #include void main(void) { char ch; printf("enter a character: "); scanf("%c", &ch); if (ch >= 65 && ch <= (65+26)) { printf("\ncapital letter\n"); } equivalent to

Principles of Programming - NI Fundamentals of Characters and Strings A string in C is an array of characters ending with the null character (‘\0’). It is written inside a double quotation mark (“ ”) A string may be assigned (in a declaration) to either a char array or to a char pointer: char color[] = “green”; OR char *color = “green”;

Principles of Programming - NI A string can also be defined by specifying the individual characters: char color[ ] = {‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘\0’}; A string is accessed via a pointer to the first character in the string. In memory, these are the characters stored: g r e e n \0 Fundamentals of Characters and Strings

Principles of Programming - NI Notice that even though there are only five characters in the word ‘green’, six characters are stored in the computer. The last character, the character ‘\0’, is the NULL character which indicates the end of the string. Therefore, if an array of characters is to be used to store a string, the array must be large enough to store the string and its terminating NULL character. Fundamentals of Characters and Strings

Principles of Programming - NI We can initialize string variables at compile time such as; char name[10] = “Arris”; This initialization creates the following spaces in storage : A r r i s \0 \0 \0 \0 \0 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Briefly review about strings :

Principles of Programming - NI Fundamentals of Characters and Strings If we happen to declare a string like this: char my_drink[3] = “tea”; We will get the following syntax error: error C2117: 'tea' : array bounds overflow Instead, we need to at least declare the array with (the size of the string + 1) to accommodate the null terminating character ‘\0’. char my_drink[4] = “tea”;

Principles of Programming - NI Example: string and ‘\0’ #include void main(void) /* a program that counts the number of characters in a string */ { char sentence[] = "I love Malaysia"; int i, count = 0; for (i = 0; sentence[i] != '\0'; i++) { count++; } printf(“%s has %d characters including the whitespace", sentence, count); } Sample output: I love Malaysia has 15 characters including the whitespace

Principles of Programming - NI Standard Functions Input scanf( ) gets( ) Standard Functions Output printf( ) puts( ) Use scanf function together with the format specifier %s for interactive input string. (no whitespace character) If the string to be read as an input has embedded whitespace characters, use standard gets function. Briefly review about strings :

Principles of Programming - NI Example: gets/puts and scanf/printf #include void main(void) { char string1[50]; char string2[50]; printf("Enter a string less than 50 characters with spaces: \n "); gets(string1); printf("\nYou have entered: "); puts(string1); printf("\nTry entering a string less than 50 characters, with spaces: \n"); scanf("%s", string2); printf("\nYou have entered: %s\n", string2); }

Principles of Programming - NI /* Sample output */ Enter a string less than 50 characters with spaces: hello world You have entered: hello world Try entering a string less than 50 characters, with spaces: hello world You have entered: hello Example cont…

Principles of Programming - NI Character Handling Library Character handling library includes several function that perform useful tests and manipulation of character data. Each function receives a character, represented as an int or EOF, as an argument. When using functions from the character handling library, the header file needs to be included. Characters in these functions are manipulated as integers (since a character is basically a 1 byte integer).

Principles of Programming - NI

Principles of Programming - NI String Conversion Functions These functions convert strings of digits to integer and floating-point values. To use these functions, the general utilities library, needs to be included. Note that these functions take a constant value as their argument. This means that we can only pass a constant string to the functions. For example: atoi (“1234”); const char *hello = “9999”; atoi(hello);

Principles of Programming - NI nPtr - The pointer to the string to be converted. endptr - The pointer to which the remainder of the string will be assigned after the conversion. We can pass a NULL if the remaining string is to be ignored. base - Indicates the format (base) of the string to be converted. If 0 is given, that means the value to be converted can be in octal (base 8), decimal (base 10) or hexadecimal (base 16).

Principles of Programming - NI Example /*1. Converting a String Into an int Using atoi. */ #include void main() { char str1[ ] = "124z3yu87"; char str2[ ] = "-3.4"; char *str3 = "e24"; printf("str1: %d\n", atoi(str1)); printf("str2: %d\n", atoi(str2)); printf("str3: %d\n", atoi(str3)); } Output: str1: 124 str2: -3 str3: 0

Principles of Programming - NI Standard Input/Output Library Functions Include to use these functions.

Principles of Programming - NI String Manipulation Functions Include to use these functions.

Principles of Programming - NI String Comparison Functions Include to use these functions

Principles of Programming - NI Example int strcmp ( const char *s1, const char *s2 ); strcmp will accept two strings. It will return an integer. This integer will either be: Negative value if s1 is less than s2. Zero value if s1 and s2 are equal. Positive value if s1 is greater than s2. strcmp is case sensitive. strcmp also passes the address of the character array to the function to allow it to be accessed.

Principles of Programming - NI char *strcat ( char *dest, const char *src ); strcat is short for string concatenate, which means to add to the end, or append. It adds the second string to the first string. It returns a pointer to the concatenated string. Make sure that the size of dest is large enough to hold the entire contents of src as well as its own contents. Example cont…

Principles of Programming - NI char *strcpy ( char *dest, const char *src ); strcpy is short for string copy, which means it copies the entire contents of src into dest. The contents of dest after strcpy will be exactly the same as src. size_t strlen ( const char *s ); strlen will return the length of a string, minus the null character ('\0'). The size_t is nothing to worry about. Just treat it as an integer that cannot be negative, which it is. Example cont…

Principles of Programming - NI #include void main() { char string1[100] = “Malaysia"; char string2[50] = “Gemilang”; strcpy(string1,string2); printf(“string1: %s\n", string1); printf(“string2: %s\n", string2); } Output : string1: Gemilang string2: Gemilang Example: strcpy

Principles of Programming - NI Example: strncpy #include void main(void) { char string1[100] = "Malaysia"; char string2[50] = "Gemilang"; strncpy(string1, string2, 4); printf(“string1: %s\n", string1); } Output: string1: Gemiysia

Principles of Programming - NI #include void main() { char name[20] = "EDDIE"; char guess[20]; int correct = 0; while(correct==0) { printf("Enter a name in uppercase: "); gets(guess); if(strcmp(name, guess)==0) { printf("Correct!\n"); correct = 1; } else printf("Try again: \n"); } Example: strcmp

Principles of Programming - NI You can only exit the program by entering "EDDIE". To perform a lowercase string comparison, use stricmp instead of strcmp but be warned: stricmp is NOT an ANSI C so it won't be supported by all C compilers. Example cont…

Principles of Programming - NI /* Concatenating Strings Using strcat */ #include void main() { char str1[50] = "Hello "; char str2[ ] = "World"; strcat(str1, str2); printf("str1: %s\n", str1); } Output: str1: Hello World Note : This only works if you've defined the str1 array to be large enough to hold the characters of your string. If you don't specify a size, the program may crash. Example: strcat

Principles of Programming - NI Example: strncat /* Concatenating Strings Using strncat */ #include void main() { char str1[50] = "Hello "; char str2[ ] = "World"; strncat(str1, str2, 2); printf("str1: %s\n", str1); } Output: str1: Hello Wo

Principles of Programming - NI Example: strlen #include void main(void) { char sentence[ ] = "I love malaysia"; int i, count = 0; count = strlen(sentence); printf("%s has %d characters including the whitespace", sentence, count); } Sample output: I love Malaysia has 15 characters including the whitespace

Principles of Programming - NI String Search Functions Include to use these functions.

Principles of Programming - NI SUMMARY C has a standard character-handling library that includes some useful functions for testing types of characters and for converting letters to uppercase and lowercase. String is another structured data type. C does not support strings as a data type. But we can use character arrays to represent strings. Standard functions printf, puts Standard functions scanf, gets String manipulation functions => to copy strings, to compare, to compute length, to concatenate T.H.E E.N.D