Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

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.
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.
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and 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.
© 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.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
 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.
 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.
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.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
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
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.
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.
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.
C Characters and Strings
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
Characters and Strings
Chapter 8 - Characters and Strings
Dale Roberts, Lecturer IUPUI
Pointers and Pointer-Based Strings
C Characters and Strings – Review Lab assignments
Arrays Strings and Parameter Passing CSCI N305
String in C++.
C++ Programming Lecture 20 Strings
Lecture 19: Working with strings
Characters and Strings Functions
Characters and Strings
C Characters and Strings
Presentation transcript:

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer

Dale Roberts Character arrays String is really a static array of characters, ex: “first” Character arrays can be initialized using string literals char string1[] = "first"; Null character '\0' terminates strings string1 actually has 6 elements It is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't', '\0' }; Can access individual characters string1[ 3 ] is character ‘s’ Array name is address of array, so & not needed for scanf char char string2[20]; scanf( "%s", string2 ); Can read a string with max of size 19 and a null character. Reads characters until whitespace (space, tab, carriage-return, newline, vertical tab) encountered Can write beyond end of array, be careful Character Arrays f i r s t \0 Null character (indicates string termination) & is NOT used, why?

Dale Roberts Fundamentals of Strings and Characters Characters Building blocks of programs Every program is a sequence of meaningfully grouped characters Character constant An int value represented as a character in single quotes 'z' represents the integer value of z Strings Series of characters treated as a single unit Can include letters, digits and special characters ( *, /, $ ) String literal (string constant) - written in double quotes "Hello" Strings are arrays of characters String a pointer to first character Value of string is the address of first character

Dale Roberts Fundamentals of Strings and Characters String declarations Declare as a character array or a variable of type char * char color[] = "blue"; char *colorPtr = "blue"; Remember that strings represented as character arrays end with '\0' color has 5 elements Inputting strings Use scanf scanf("%s", word); Copies input into word[] Do not need & (because a string is a pointer) Remember to leave room in the array for '\0'

Dale Roberts Command-Line Arguments Example: print out the arguments. ex: hello world main (int argc, char *argv[]) { int i; int i; for (i = 1; i < argc; i++) for (i = 1; i < argc; i++) printf(“%s%c”, argv[i], (i < argc-1) ? ‘ ’ : ‘\n’); printf(“%s%c”, argv[i], (i < argc-1) ? ‘ ’ : ‘\n’);} main (int argc, char *argv[]) { while (--argc > 0) while (--argc > 0) printf(“%s%c”, *++argv, (argc > 1) ? ‘ ’ : ‘\n’); printf(“%s%c”, *++argv, (argc > 1) ? ‘ ’ : ‘\n’);} main (int argc, char *argv[]) { while (--argc > 0) while (--argc > 0) printf((argc > 1) ? “%s “ ; “%s\n“, *++argv); printf((argc > 1) ? “%s “ ; “%s\n“, *++argv);}

Dale Roberts String Manipulation Functions String handling library has functions to Manipulate string data Search strings Tokenize strings Determine string length

Dale Roberts 1. Initialize variables 2. Function calls 3. Print Program Output 1/* Fig. 8.19: fig08_19.c 2 Using strcat and strncat */ 3#include 4#include 5 6int main() 7{7{ 8 char s1[ 20 ] = "Happy "; 9 char s2[] = "New Year "; 10 char s3[ 40 ] = ""; printf( "s1 = %s\ns2 = %s\n", s1, s2 ); 13 printf( "strcat( s1, s2 ) = %s\n", strcat( s1, s2 ) ); 14 printf( "strncat( s3, s1, 6 ) = %s\n", strncat( s3, s1, 6 ) ); 15 printf( "strcat( s3, s1 ) = %s\n", strcat( s3, s1 ) ); 16 return 0; 17} s1 = Happy s2 = New Year strcat( s1, s2 ) = Happy New Year strncat( s3, s1, 6 ) = Happy strcat( s3, s1 ) = Happy Happy New Year

Dale Roberts Other Functions of the String Handling Library char *strerror( int errornum ); Creates a system-dependent error message based on errornum Returns a pointer to the string size_t strlen( const char *s ); Returns the number of characters (before NULL ) in string s 1/* Fig. 8.37: fig08_37.c 2 Using strerror */ 3#include 4#include 5 6int main() 7{7{ 8 printf( "%s\n", strerror( 2 ) ); 9 return 0; 10} No such file or directory 1. Function call 2. Print Program Output

Dale Roberts Comparing Strings A useful operation is the comparison of two strings. Two strings are related in the same three basic ways as number values. One string is either less than, equal to, or greater than the other. String comparison is usually based on the positions of the characters in the character set. Scanning along both strings and comparing corresponding characters establish the relationship between two strings. The strings are equal as long as corresponding characters are equal. If two characters are different, the comparisons are based on their relative order in the character set. The character whose code is less belongs to the lesser string. Ex. “abcd” < “abcz” If the two strings are of different length, but identical up to the end of the shorter one, then the shorter string is the lesser of the two: Ex. “abc” < “abcd” If the two strings are of different length and consist of Upper and lowercase letters, Upper case letters come before lower case letter and a blank has a lower value than all other letters. Ex.“AZZZ” < “Aaaah” Below is an example of a comparison of strings that contain blanks. Scanning along both strings and comparing corresponding characters, you see the strings are equal for the first two characters. You then compare the blank and the t; you then reach the conclusion below. Ex.“hi there” < “hit a ball”

Dale Roberts Comparison Functions Comparing strings Computer compares numeric ASCII codes of characters in string Appendix D has a list of character codes int strcmp( const char *s1, const char *s2 ); Compares string s1 to s2 Returns a negative number if s1 s2 int strncmp( const char *s1, const char *s2, size_t n ); size_t n ); Compares up to n characters of string s1 to s2 Returns values as above