Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Similar documents

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

C programming basics T3-1 -

Introduction to string

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Principles of C and Memory Management

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture7 Strings and Characters

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture5 C Characters and Strings

Fundamental of Programming (C)

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

8. Characters, Strings and Files

Functions: call by reference vs. call by value

CS107, Lecture 4 C Strings

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Chapter 11 Strings and Files. Starting Out with Games & Graphics in C++ Tony Gaddis

Create a Program in C (Last Class)

Intermediate Programming, Spring 2017*

Engineering Problem Solving with C++, Etter

C Characters and Strings

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Chapter 13. Strings. Introduction

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

CSC 2400: Computer Systems. Arrays and Strings in C

CS 222: Pointers and Manual Memory Management

Computers Programming Course 10. Iulian Năstac

Chapter 10. Arrays and Strings

Introduction to C/C++ Lecture 5 - String & its Manipulation

CSC 2400: Computer Systems. Arrays and Strings in C

C Programming Language

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Computer Programming: Skills & Concepts (CP) Strings

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Week 8 Lecture 3. Finishing up C

Lecture 17. Strings II. CptS 121 Summer 2016 Armen Abnousi

Personal SE. Functions, Arrays, Strings & Command Line Arguments

CS106L Handout #05 Fall 2007 October 3, C Strings

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

ARRAYS(II Unit Part II)

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

Procedural Programming & Fundamentals of Programming

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Chapter 8 Character Arrays and Strings

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

ONE DIMENSIONAL ARRAYS

Chapter 8: Character & String. In this chapter, you ll learn about;

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Structured programming

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

CS107 Spring 2019, Lecture 4 C Strings

Lecture 12. We have already used strings. Strings. Hello Class is a string.

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

Fundamentals of Programming & Procedural Programming

Write a C program using arrays and structure

Computers Programming Course 11. Iulian Năstac

211: Computer Architecture Summer 2016

Contents. A Review of C language. Visual C Visual C++ 6.0

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Procedural Programming

CSE 333 Midterm Exam 2/14/14

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

CS 103 Lab 6 - Party Like A Char Star

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

CS 103 Lab - Party Like A Char Star

CMPSC 311- Introduction to Systems Programming Module: Strings

String can be represented as a single-dimensional character type array. Declaration of strings

Built-in Functions for NTCAs.

Course organization. Course introduction ( Week 1)

Chapter 4 (week 5) Section 4.10 Pointer and Dynamic Memory Allocation. CS 50 Hathairat Rattanasook

Object Oriented Pragramming (22316)

CS1100 Introduction to Programming

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Arrays and Strings. CS449 Fall 2017

CS 31 Discussion: Week 6. Taylor Caulfield

Fundamentals of Programming Session 19

CSE 333 Midterm Exam 7/27/15 Sample Solution

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

EE 355 Lab 4 - Party Like A Char Star

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

C-String Library Functions

Chapter 9 Strings. With this array declaration: char s[10];

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

CS 261 Fall Mike Lam, Professor. Structs and I/O

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Important Questions for Viva CPU

Transcription:

Chapter 5 Section 5.4 The Common String Library Functions CS 50 Hathairat Rattanasook

Library Functions We already discussed the library function fgets() Library functions are available: to find the length of a string: strlen() to make a copy of a string: strcpy() to concatenate one string to the end of another: strcat() to compare two strings: strcmp() CS 50 - Hathairat Rattanasook 1

strlen() It calculates the length of a string. The first parameter for strlen() is the address of a string, that is the first char of a null-terminated string. The return type is size_t which is a type in ANSI C that is used to specify sizes or lengths in bytes. It is compatible with the integer types. CS 50 - Hathairat Rattanasook 2

strlen() - Example 1 char *str = "This is a string"; 2 int length; 3 length = strlen("hello World!"); 4 printf("length: %d\n", length); // prints 12 5 length = strlen(str); 6 printf("length: %d\n", length); // prints 16 Line 3: Returns the length of "Hello World!" which is 12 Line 5: Returns the length of str pointer which is 16 CS 50 - Hathairat Rattanasook 3

strcpy() It makes a copy of a string. It takes two parameters: The first parameter is the address of a char array where the copied string will be written to. The memory space has to be allocated before calling this function. The second parameter is the first char of a nullterminated string. WARNING: strcpy() requires that enough memory has been allocated for the first parameter! CS 50 - Hathairat Rattanasook 4

strcpy() - Example 1 char copy[64]; 2 char *original = "C is fun!"; 3 strcpy(copy, original); 4 printf("%s\n", copy); 5 strcpy(copy, "C++ is fun too!"); 6 printf("%s\n", copy); Line 3: Copies the string original to the char array copy. Line 5: Copies the "C++ is fun too!" to char array copy. NOTE: The copy variable is declared as an array which allocates memory for 64 characters including the null terminated character. CS 50 - Hathairat Rattanasook 5

strcat() It appends one string to the end of another string. It takes two parameters: The first parameter is the address of an array which contains a null-terminated string. The array must be allocated with enough free space to fit the string that is passed in as the second parameter. The second parameter is the first char of a nullterminated string which will be appended to the first parameter. CS 50 - Hathairat Rattanasook 6

strcat() - Warning WARNING: Just like strcpy() and strcat() requires that enough memory has been allocated for the first parameter! If there is not enough free space in the first parameter to fit the second string, strcat() will continue to write to memory and deleting whatever is currently at that memory location. CS 50 - Hathairat Rattanasook 7

strcat() - Example 1 char str[64] = "one two"; 2 strcat(str, "three four"); 3 printf("%s\n", str); Line 1: Creates a char array containing "one two" and additional space allocated to fit another string Line 2: Concatenates the string "three four" to the string "one two" that is provided as first argument. Line 3: prints "one twothree four" NOTE: The str variable is declared as an array which allocates memory for 64 characters CS 50 - Hathairat Rattanasook 8

It compares two strings. It takes two parameters: strcmp() The first parameter is a char pointer of a null-terminated string. The second parameter is also a char pointer of a null-terminated string. The return value is: 0 if both strings are identical A negative number if the first string is lexicographically less than the second string A positive number if the first string is lexicographically greater than the second string CS 50 - Hathairat Rattanasook 9

strcmp() - Example 1 char *str = "Hello", *str2 = "Hello"; 2 int result = strcmp("this is a string", "This is a STRING"); 3 printf("%d\n", result); 4 result = strcmp(str, str2); 5 printf("%d\n", result); Line 2: compares two strings which differ by the capital word STRING. Line 3: Prints 32 because the first string is lexicographically greater than the second string Line 5: Prints 0 which means that both strings are equal. CS 50 - Hathairat Rattanasook 10

Question? 1. What is the difference between? char *str = "Some string"; char str[64] = "Some string"; 2. Can you concatenate "abc" and "def"? 3. What does it mean if strcmp() returns a negative number? CS 50 - Hathairat Rattanasook 11

Answers 1. What is the difference between? char *str = "Some string"; // fixed sized string char str[64] = "Some string"; /* string with "Some string" but additional allocated free memory space to fit additional chars */ 2. Can you concatenate "abc" and "def"? char abc[7] = "abc", *def = "def"; strcat(abc, def); printf("%s\n", abc); 3. What does it mean if strcmp() returns a negative number? A negative number means the first string is lexicographically less than the second string, eg. strcmp("abc", "def") return -3 CS 50 - Hathairat Rattanasook 12