Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Introduction to C Programming
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
 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.
Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
 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 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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
 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.
1 CSE1303 Part A Data Structures and Algorithms Semester 2, 2006 Lecture A1 – Welcome & Revision.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
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.
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.
CS Nov 2006 C-strings.
 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.
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.
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)
Strings in C are my friend, and they can be yours, too. An inspiring and epic tale by Erik Speyer, John Sullivan, and Dane Bennington.
1 Homework HW6 On line – due next class Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Review of Lectures 12, 13, 14 -Functions -Arrays -Strings.
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:
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
String Array (Multidimensional Arrays) 1. A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string];
Charles Clute Tom Most Michael Hein. Strings in C  There is no String... But there’s hope! Strings are character arrays char volume[6]; char volume[6]
5.6 String Processing Part 2. Sprintf(destnvar,…..regularprintf) Write formatted data to string Same as printf except the output is put in variable. A.
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).
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.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
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.
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.
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.
INC 161 , CPE 100 Computer Programming
C Characters and Strings
CSE 303 Lecture 14 Strings in C
Computer science C programming language Lesson 5
C Programming:Part 3 Characters and Strings File Processing Exercise.
Strings #include <stdio.h>
Characters and Strings Functions
C Characters and Strings
Presentation transcript:

Character String Manipulation

Overview Character string functions sscanf() function sprintf() function

Some Character String Functions double atof(const char *string); - Converts string into a floating point value int atoi(const char *string); - Converts string into an int value char *strcat(char *s1, const char *s2); - Appends s2 onto the end of s1 char *strchr(const char *s, int c); - Searches for first occurrence of c in s int strcmp(const char *s1, const char *s2); - Compares s1 to s2 char *strcpy(char *s1, const char *s2); - Copies s2 onto s1 size_t strlen(const char *s); - Returns the number of characters in s char *strstr(const char *s1, const char *s2); - Searches for s2 in s1 char *strtok(char *s1, const char *s2); - Extracts tokens from string s1 based on token separators in s2

sscanf() Function #include int sscanf(const char *buffer, const char *format, …); The sscanf() function is identical to scanf() except that data is read from the array pointed to by buffer rather than stdin The return value is equal to the number of variables that were actually assigned values –A value of zero means that no fields were assigned any values

sprintf() Function #include int sprintf(char *buffer, const char *format, …); The sprintf() function is identical to printf() except that the output is put into the array pointed to by buffer instead of being written to stdout The array pointed to by buffer should be null terminated The return value is equal to the number of characters actually placed into the array The sprintf() function provides no bounds checking on the array pointed to by buffer

Example use of strcpy(), sscanf() and sprintf() #include #define MAX_LENGTH 50 int main(void) { char stringA[MAX_LENGTH]"; char stringB[MAX_LENGTH]; int count; float costPerItem; int binNbr; char name[MAX_LENGTH]; float totalCost; (More on next slide)

Example use strcpy(), sscanf() and sprintf() strcpy(stringA, " bottle“); sscanf(stringA, "%d %f %d%s", &count, &costPerItem, &binNbr, name); fprintf(stderr, "Input data: %d %.2f %d %s\n\n", count, costPerItem, binNbr, name); totalCost = count * costPerItem; sprintf(stringB, "%d %s items * $ %.2f per %s = $ %.2f", count, name, costPerItem, name, totalCost); printf("Computation for Bin # %d:\n %s\n", binNbr, stringB); return 0; } // End main

Sample output Input data: bottle Computation for Bin # 35: 103 bottle items * $ per bottle = $