Fundamentals of Characters and Strings

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.
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.
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 10.
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 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
 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
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.
 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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
1 Lecture 12 Pointers and Strings Section 5.4, ,
1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization 5.3Pointer Operators 5.4Calling Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
 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.
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.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
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,
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.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
C Characters and Strings
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Chapter 8 - Characters and Strings
Pointers and Pointer-Based Strings
Object Oriented Programming COP3330 / CGS5409
Remark: Data Type of Array Name
Pointers and Pointer-Based Strings
Strings A collection of characters taken as a set:
C Characters and Strings – Review Lab assignments
String in C++.
String What it is Why it’s useful
Chapter 5 - Pointers and Strings
CPS120: Introduction to Computer Science
5.1 Introduction Pointers Powerful, but difficult to master
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 20 Strings
Lecture 19: Working with strings
Strings #include <stdio.h>
Lecture 2 Arrays & Pointers September 7, 2004
Characters and Strings Functions
Characters and Strings
C Characters and Strings
Presentation transcript:

5.12.1 Fundamentals of Characters and Strings Character constant Integer value of a character Single quotes 'z' is the integer value of z, which is 122 String Series of characters treated as one unit Can include letters, digits, special characters +, -, * ... String literal (string constants) Enclosed in double quotes, for example: "I like C++" Array of characters, ends with null character '\0' Strings are constant pointers (like arrays) Value of string is the address of its first character

5.12.1 Fundamentals of Characters and Strings String assignment Character array: char color[] = "blue"; Creates 5 element char array, color, (last element is '\0') variable of type char * char *colorPtr = "blue"; Creates a pointer to string "blue", colorPtr, and stores it somewhere in memory

5.12.1 Fundamentals of Characters and Strings Reading strings Assign input to character array word[ 20 ] cin >> word Reads characters until whitespace or EOF String could exceed array size cin >> setw( 20 ) >> word; Reads 19 characters (space reserved for '\0') cin.getline Reads a line of text Using cin.getline cin.getline( array, size, delimiter character);

5.12.1 Fundamentals of Characters and Strings cin.getline Copies input into specified array until either One less than the size is reached The delimiter character is input Example char sentence[ 80 ]; cin.getline( sentence, 80, '\n' );

5.12.2 String Manipulation Functions of the String-handling Library String handling library <cstring> provides functions to Manipulate strings Compare strings Search strings Tokenize strings (separate them into logical pieces) ASCII character code Strings are compared using their character codes Easy to make comparisons (greater than, less than, equal to) Tokenizing Breaking strings into tokens, separated by user-specified characters Tokens are usually logical units, such as words (separated by spaces) "This is my string" has 4 word tokens (separated by spaces)

5.12.2 String Manipulation Functions of the String-handling Library char *strcpy( char *s1, const char *s2 ); Copies the string s2 into the character array s1. The value of s1 is returned. char *strncpy( char *s1, const char *s2, size_t n ); Copies at most n characters of the string s2 into the character array s1. The value of s1 is returned. char *strcat( char *s1, const char *s2 ); Appends the string s2 to the string s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned. char *strncat( char *s1, const char *s2, size_t n ); Appends at most n characters of string s2 to string s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.

5.12.2 String Manipulation Functions of the String-handling Library (III) int strcmp( const char *s1, const char *s2 ); Compares the string s1 with the string s2. The function returns a value of zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively. int strncmp( const char *s1, const char *s2, size_t n ); Compares up to n characters of the string s1 with the string s2. The function returns zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively. size_t strlen( const char *s ); Determines the length of string s. The number of characters preceding the terminating null character is returned.

String Functions To use string functions we must include the <cstring> library or <string.h> library in the top of the program #include <cstring> String functions that we will cover are: strcpy strncpy strcat Strncat Strcmp Strncmp strlen

Example on strcpy, strncpy #include <iostream.h> #include<string.h> void main( ) { char x[ ]="Happy Birthday to You"; char y[25]; char z[15]; strcpy(y, x); cout<<x<<" "<<y<<endl; strncpy(z, x,14); //z[14]='\0'; // strncpy doesn’t add \0 at the end cout<<z<<endl; }

Output Happy Birthday to You Happy Birthday to You Happy Birthday Press any key to continue

Example on strcat, strncat #include <iostream.h> #include<string.h> void main( ) { char s1[20]="Happy"; char s2[ ]="New Year"; char s3[40]=""; // this puts \0 in the whole array cout<<s1<<" "<<s2<<endl; strcat(s1, s2); strncat(s3, s1, 5); cout<<s1<<" "<<s3<<endl; strcat(s3, s1); }

Output Happy New Year HappyNew Year New Year HappyNew Year Happy HappyNew Year HappyHappyNew Year Press any key to continue

#include <iostream.h> #include<string.h> void main( ) { char s1[ ]="Happy New Year"; char s2[ ]="Happy New Year"; char s3[ ]="Happy Holidays"; cout<<s1<<"\n"<<s2<<"\n"<<s3<<"\n"; cout<<strcmp(s1, s2)<<"\n" <<strcmp(s1, s3)<<"\n" <<strcmp(s3, s1)<<endl; cout<<strncmp(s1,s3,6)<<"\n" <<strncmp(s1, s3, 7)<<"\n" <<strncmp(s3, s1,7)<<endl; }

Output Happy New Year Happy Holidays 1 -1 Press any key to continue

The original value of number is 5 The new value of number is 125

Difference between arrays of char and arrays of int #include <iostream.h> void main() { // char a[10]={‘A’,’h’,’m’,’a’,’d’}; char a[10]="Ahmad"; cout<<a<<endl; // prints Ahmed int b[10]={1,2,3,4,5}; cout<<b<<endl; // prints the address of //the first element in the array }