CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections 8.1 - 8.7.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
Strings string.h library. String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy.
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 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
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.
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.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
 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.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
C strings (Reek, Ch. 9) 1CS 3090: Safety Critical Programming in C.
CS Nov 2006 C-strings.
Characters & Strings Lesson 2 CS1313 Spring Characters & Strings Lesson 2 Outline 1.Characters & Strings Lesson 2 Outline 2.Character String Declaration.
 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.
Introduction to C programming
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
 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.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
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.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
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’};
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
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.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Sahar Mosleh California State University San MarcosPage 1 Character String.
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 operations. About strings To handle strings more easily, we need to include a library> #include To see what the library allows us to do, look here:
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.
Strings, Slide Fundamental Programming Strings.
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
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.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
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.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
Fundamentals of Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
C Stuff CS 2308.
Strings #include <stdio.h>
Characters and Strings Functions
C Characters and Strings
Presentation transcript:

CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections

CMSC 104, Version 8/062L25Strings.ppt String A string is a character array that has a marker to show where the data ends, when the array is larger than the data. char firstname[ 50 ] = “Sue”; The array firstname is 50 characters long, but the data is only four characters long. You must count the marker. The marker is character that is set to the number zero. (Sometimes called a null terminator.)

CMSC 104, Version 8/063L25Strings.ppt Array Constraint There are now built-in operators to manipulate arrays, except to initialize them when you declare the array: char firstname[ 50 ] = “Sue”;

CMSC 104, Version 8/064L25Strings.ppt Missing Operators There is no string assignment operators. There are not string comparison operators. There are not string combination operators. However, there are built-in functions to do this common tasks.

CMSC 104, Version 8/065L25Strings.ppt Built-in String Functions String assignment: strcpy( destination, source ) char name[ 25 ]; /* contains nothing */ strcpy( name, “Hilton” ); /* name now contains “Hilton” */

CMSC 104, Version 8/066L25Strings.ppt Built-in String Functions (cont’d) String comparison: strcmp( strA, strB ); o If strA comes after strB, the function returns a positive number. o Is strB comes last, the function returns a negative number. o If strA and strB are the same thing, the function returns a zero. result = strcmp( “CMSC”, “IFSM” ); /* negative */ result = strcmp( “IFSM”, “CMSC” ); /* positive */ result = strcmp( “CSMC”, “CMSC” ); /* zero */

CMSC 104, Version 8/067L25Strings.ppt Built-in String Functions (cont’d) String combination: strcat( destination, source ) o The source is not changed. o The destination contain exactly what it had before plus what was in the source. Nothing else is added. NOTE: If you are combining a first name and last name for a full name, you must use another strcat to add the space between them: strcpy( fullName, firstName); strcat( fullName, “ “ ); strcat( fullName, lastName );

CMSC 104, Version 8/068L25Strings.ppt Built-in String Functions (cont’d) Extracting words (tokens) from a string: /* get the first token (delimited by a blank) */ printf( "%s\n", strtok( b, " " ) ); /* This is more useful after you learn to use pointers. */

CMSC 104, Version 8/069L25Strings.ppt Built-in String Functions (cont’d) What if I want to get a menu choice, that is the numbers 1 to 4 or the char ‘q’? Use getchar( ) to get the menu choice, check for ‘q’ and if it is not, then convert it to a number. /* convert a string (ASCII) to an integer */ printf( "%d\n", atoi( "1234" ) ); /* convert a string (ASCII) to a float */ printf( "%f\n", atof( " " ) );

CMSC 104, Version 8/0610L25Strings.ppt Built-in String Functions (cont’d) How long is the data in the string (not counting the null terminator)? stringSize = strlen( strA );

CMSC 104, Version 8/0611L25Strings.ppt String Libraries #include files: #include /* needed by atoi( ) and atof( ) */ #include /* needed by str...( ) functions */

CMSC 104, Version 8/0612L25Strings.ppt Sample Program #include #include /* needed by atoi( ) and atof( ) */ #include /* needed by str...( ) functions */

CMSC 104, Version 8/0613L25Strings.ppt Sample Program (cont’d) int main( void ) { char a[ 100 ] = "Excellence"; char b[ 100 ]; char c[ 100 ] = "Failure"; int result;

CMSC 104, Version 8/0614L25Strings.ppt Sample Program (cont’d) /* Make sure the array a is as expected */ printf( "string a is >%s<\n", a ); /* assign a to b */ strcpy( b, a ); printf( "After strcpy(b, a), string b is now >%s<\n", b );

CMSC 104, Version 8/0615L25Strings.ppt Sample Program (cont’d) printf( "\n=============\nString b = >%s< and is %d characters long\n", b, strlen( b ) ); /* put in a space and add the array a to what is in array a */ strcat ( b, " " ); printf( "After strcat(b, \" \"), string b = >%s< and is %d characters long\n", b, strlen( b ) ); strcat ( b, a ); printf( "After strcat(b, a), string b = >%s< and is %d characters long\n", b, strlen( b ) );

CMSC 104, Version 8/0616L25Strings.ppt Sample Program (cont’d) /* get the first token (delimited by a blank) */ printf( "strtok( b, \" \" ) gives %s\n", strtok( b, " " ) ); printf( "\n=============\n"); printf( "string a = %s string c = %s\n", a, c ); /* "Excellence" comes before "Failure", so print a negative number */ printf( "strcmp( a, c ) gives %d\n", strcmp( a, c ) );

CMSC 104, Version 8/0617L25Strings.ppt Sample Program (cont’d) /* " Failure " comes before “Excellence ", so print a positive number */ printf( "strcmp( c, a ) gives %d\n", strcmp( c, a ) ); /* "Excellence" is the same as "Excellence", so print zero */ printf( "strcmp( a, \"Excellence\" gives %d\n", strcmp( a, "Excellence" ) );

CMSC 104, Version 8/0618L25Strings.ppt Sample Program (cont’d) result = strcmp( "CMSC", "IFSM" ); /* negative */ printf( "After strcmp( \"CMSC\", \"IFSM\" ), result is %d\n", result); result = strcmp( "IFSM", "CMSC" ); /* positive */ printf( "After strcmp( \"IFSM\", \"CMSC\" ), result is %d\n", result); result = strcmp( "CMSC", "CMSC" ); /* zero */ printf( "After strcmp( \"CMSC\", \"CMSC\" ), result is %d\n", result);

CMSC 104, Version 8/0619L25Strings.ppt Sample Program (cont’d) printf( "\n=============\n"); /* convert a string to an integer */ printf( "atoi( \"1234\" gives %d\n", atoi( "1234" ) ); /* convert a string to a float */ printf( "atof( \" \" ) gives %f\n", atof( " " ) ); return 0; }

CMSC 104, Version 8/0620L25Strings.ppt Sample Program Output string a is >Excellence< After strcpy(b, a), string b is now >Excellence< ============= String b = >Excellence< and is 10 characters long After strcat(b, " "), string b = >Excellence < and is 11 characters long After strcat(b, a), string b = >Excellence Excellence< and is 21 characters long strtok( b, " " ) gives Excellence

CMSC 104, Version 8/0621L25Strings.ppt Sample Program Output (cont’d) ============= string a = Excellence string c = Failure strcmp( a, c ) gives -1 strcmp( c, a ) gives 1 strcmp( a, "Excellence" gives 0 After strcmp( "CMSC", "IFSM" ), result is -1 After strcmp( "IFSM", "CMSC" ), result is 1 After strcmp( "CMSC", "CMSC" ), result is 0

CMSC 104, Version 8/0622L25Strings.ppt Sample Program Output (cont’d) ============= atoi( "1234" gives 1234 atof( " " ) gives