Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.

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

Problem Solving & Program Design in C
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.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
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 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 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Topic 10 - Strings.
© 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.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 12: More About.
CS Nov 2006 C-strings.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
1. An array is a collection of a fixed number of components wherein all of the components are of the same type Example: Suppose that there is a list of.
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 Chapter 10 Characters, Strings, and the string class.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
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.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Characters and Strings File Processing Exercise C Programming:Part 3.
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.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Characters, Strings, And The string Class Chapter 10.
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:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
chap9 Chapter 9 Strings chap9 2 Strings A data structure deals with a grouping of characters. C implements the string data structure.
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
Strings, Pointers and Tools
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology.
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
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.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Strings CSCI 112: Programming in C.
C Characters and Strings
CSE 303 Lecture 14 Strings in C
Chapter 8 Character Arrays and Strings
Strings #include <stdio.h>
C Characters and Strings
Computer Science II CS132/601* Lecture #C-3.
Presentation transcript:

Chapter 8 Strings

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of type char. stringSince string is an array, the declaration of a string is the same as declaring a char array. –char string_var[30]; –char string_var[20] = “Initial value”;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-3 Memory Storage for a String null character ‘\0’The string is always ended with a null character ‘\0’. The characters after the null character are ignored. e.g., char str[20] = “Initial value”; nitialvalue??…I \0 [0][13]

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-4 Input/Output of a String %sThe placeholder %s is used to represent string arguments in printf and scanf. –printf(“Topic: %s\n”, string_var); The string can be right-justified by placing a positive number in the placeholder. %8s –printf(“%8s”, str); The string can be left-justified by placing a negative number in the placeholder. %-8s –Printf(“%-8s”, str);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-5 Right and Left Justification of Strings %8s The “%8s” placeholder displays a string which is right- justified and in 8-columns width. If the actual string is longer than the width, the displayed field is expanded with no padding.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-6 An Example of Manipulating String with scanf and printf The dept is the initial memory address of the string argument. Thus we don’t apply the & operator on it.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-7 Execution of scanf ("%s", dept); Whenever encountering a white space, the scanning stops and scanf places the null character at the end of the string. e.g., if the user types “MATH 1234 TR 1800,” the string “MATH” along with ‘0’ is stored into dept.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-8 String Library Functions The string can not be copied by the assignment operator ‘=’. –e..g, “ str = “Test String” ” is not valid. C provides string manipulating functions in the “string.h” library.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-9 Some String Functions from String.h FunctionPurposeExample strcpy Makes a copy of a string strcpy(s1, “Hi”); strcat Appends a string to the end of another string strcat(s1, “more”); strcmp Compare two strings alphabetically strcmp(s1, “Hu”); strlen Returns the number of characters in a string strlen(“Hi”) returns 2. strtok Breaks a string into tokens by delimiters. strtok(“Hi, Chao”, “,”);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-10 Functions strcpy and strncpy Function strcpy copies the string in the second argument into the first argument. –e.g., strcpy(dest, “test string”); null character –The null character is appended at the end automatically. –If source string is longer than the destination string, the overflow characters may occupy the memory space used by other variables. Function strncpy copies the string by specifying the number of characters to copy. –You have to place the null character manually. dest[6] = ‘\0’; –e.g., strncpy(dest, “test string”, 6); dest[6] = ‘\0’; –If source string is longer than the destination string, the overflow characters are discarded automatically.

String Assignment

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-12 Extracting Substring of a String (1/2) We can use strncpy to extract substring of one string. –e.g., strncpy(result, s1, 9);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-13 Extracting Substring of a String (2/2) &s1[5]e.g., strncpy(result, &s1[5], 2);

1-14 Figure 8.7 Program Using strncpy and strcpy Functions to Separate Compounds into Elemental Components

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-15 Functions strcat and strlen Functions strcat and strncat concatenate the fist string argument with the second string argument. –strcat(dest, “more..”); –strncat(dest, “more..”, 3); Function strlen is often used to check the length of a string (the number of characters before the fist null character).

String Concatenation Functions strcat and strncat concatenate the fist string argument with the second string argument. char f1[15] = "John"; char f2[15] = "Jacqueline"; char f3[15] = "Bob"; char last[15] = "Kennedy"; strcat(f1, last); puts(f1); strcat(f2, last); /* invalid overflow */ puts(f2); strncat(f3, last, 5); puts(f3);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-17 Distinction Between Characters and Strings The representation of a char (e.g., ‘Q’) and a string (e.g., “Q”) is essentially different. –A string is an array of characters ended with the null character. Q Character ‘Q’ Q\0 String “Q”

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-18 String Comparison (1/2) Suppose there are two strings, str1 and str2. initial memory address –The condition compare the initial memory address of str1 and of str2. The comparison between two strings is done by comparing each corresponding character in them. –The characters are comapared against the ASCII table. –“thrill” < “throw” since ‘i’ < ‘o’; –“joy” < joyous“; The standard string comparison uses the strcmp and strncmp functions.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-19 String Comparison (2/2) RelationshipReturned ValueExample str1 < str2 Negative“Hello”< “Hi” str1 = str2 0“Hi” = “Hi” str1 > str2 Positive“Hi” > “Hello” e.g., we can check if two strings are the same by if(strcmp(str1, str2) != 0) printf(“The two strings are different!”);

strcmp example Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-20 #include int main() { char s1[10] = "SAM", s2[10]="SAM" ; int len; len = strcmp (s1,s2); if (len == 0) printf ("Two Strings are Equal"); return 0; }

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-21 Input/Output of Characters and Strings The stdio library provides getchar function which gets the next character from the standard input. –“ ch = getchar(); ” is the same as “ scanf(“%c”, &ch); ” –Similar functions are putchar, gets, puts.

getchar Example Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-22 int main( ) { char ch; ch = getchar(); printf("Accepted Character : %c",ch); return 0; }

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-23 Character Analysis and Conversion The library defines facilities for character analysis and conversion. FunctionsDescription isalpha Check if the argument is a letter isdigit Check if the argument is one of the ten digits isspace Check if argument is a space, newline or tab. tolower Converts the uppercase letters in the argument to lowercase letters. toupper Converts the lowercase letters in the argument to uppercase letters.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-24 #include int main() { char var1 = 'd'; char var2 = '\t'; if( isalpha(var1) ) { printf("var1 is an alphabet letter\n", var1 ); } else{ printf("var1 is not an alphabet letter\n", var1 ); } if( isalpha(var2) ){ printf("var2 is an alphabet letter\n", var2 ); } else{ printf("var2 is not an alphabet letter\n", var2 ); } return(0); } isalpha Example

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-25 int main() { char ch = '1'; if( isdigit (ch)) printf("\nThis is Digit"); else printf("\nThis is not a Digit"); return 0; } isdigit Example

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-26 int main() { char var = ' '; if( isspace(var) ) { printf("it is a white-space character\n", var ); } else { printf ("it is not a white-space character",var ); } return 0; } isspace Example

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-27 #include int main() { int i = 0; char c; char str[] = "MALTEPE"; while( str[i] ) { putchar(tolower(str[i])); i++; } return(0); } tolower Example

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-28 Conversions Between Strings Numbers The defines some basic functions for conversion from strings to numbers: –atoi(“123”) –atoi(“123”) converts a string to an integer. –atol(“123”) –atol(“123”) converts a string to a long integer. –atof(“12.3”) –atof(“12.3”) converts a string to a float. However, there is no functions such as itoa, itof, …etc, sprintf –because there is a function called sprintf which can converts many formats to a string.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-29 #include int main() { int val; char str1[3]="44"; char str2[3]="66"; val = atoi(str1)+atoi(str2); printf("Total = %d\n", val); return(0); }

strtok example Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-30 int main() { char string[] = "a string,of,,tokens"; char *token; token = strtok(string,","); /*There are two delimiters here*/ while (token != NULL){ printf("The token is: %s\n", token); token = strtok(NULL,","); } return 0; }

strtok example Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-31

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-32 Arrays of Strings An array of strings is a two-dimensional array of characters in which each row is one string. –char month[5][10] = {“January”, “February”, “March”, “April”, “May”};