CS 1400 27 Nov 2006 C-strings.

Slides:



Advertisements
Similar presentations
 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.
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 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
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.
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.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
CS 1400 Chapter 10 Strings. Character testing library #include bool isalpha (char c); bool isalnum (char c); bool isdigit (char c); bool islower (char.
1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
CS 1400 Apr 18, 2007 Chapter 10 Strings. Character testing library #include bool isalpha (char c); bool isalnum (char c); bool isdigit (char c); bool.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 12: More About.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
Data Structure and C Part-6. Naming a Function Any valid variable-name can be given to the user-defined function. The main program itself is considered.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
1 Chapter 10 Characters, Strings, and the string class.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
Lecture 17: Characters, Strings, and the string class Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
 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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
STARTING OUT WITH STARTING OUT WITH Class 9 Honors.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
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’};
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
Characters, Strings, And The string Class Chapter 10.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
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:
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
Chapter Characters, Strings, and the string class 10.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
Jordan Jozwiak CS50. Announcements Pset3 will be returned by 7pm on Tuesday REMINDER: Access section materials from this year and last year at
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
C Characters and Strings
C-Strings We have already seen that a C-string is a null-terminated array of characters.
C Characters and Strings
Characters, C-Strings, and More About the string Class
CSE 303 Lecture 14 Strings in C
Chapter 8 - Characters and Strings
Chapter 12: More on C-Strings and the string Class
Standard Version of Starting Out with C++, 4th Edition
C Stuff CS 2308.
10.1 Character Testing.
Standard Version of Starting Out with C++, 4th Edition
Strings #include <stdio.h>
C Characters and Strings
Presentation transcript:

CS 1400 27 Nov 2006 C-strings

Character Testing… #include <cctype> Functions: (returning true or false) bool isalpha (char c); bool isalnum (char c); bool isdigit (char c); bool islower (char c); bool isupper (char c); bool isprint (char c); bool ispunct (char c); bool isspace (char c);

Character Conversion… #include <cctype> Functions: (returning a char result) char toupper (char c); char tolower (char c); Example: char c; cin >> c; cout << toupper(c);

C-strings with char arrays… An older, traditional way of representing strings declaration: char name[30], line[30]; null char termination cin >> name; cout << name; cout << “hello world!”; no direct assignment! line = name; no direct comparison! if (name < line)…

C-strings with char arrays… Functions: int strlen (char *str); Returns the length of the C-string in array str. Example: char word[30]; cout << “word length: “ << strlen(word);

C-strings with char arrays… Functions: void strcat (char *str1, char *str2); Concatenates str2 onto the end of str1. Example: char word[30], line[80]; cin >> line; cin >> word; strcat (line, word); cout << “complete line: “ << line;

C-strings with char arrays… Functions: void strcpy (char *dest, char *source); void strncpy (char *dest, char *source, int length); Copies source into dest array (including null char). Example: char word[30], line[80]; cin >> word; strcpy (line, word); cout << “copied word: “ << line; strncpy (line, word, 5); cout << “first 5 chars of word: “” << line;

C-strings with char arrays… Functions: int strcmp (char *str1, char *str2); Compares strings in str1 and str2, returning <0, 0, or >0 Example: char word[30], line[80]; cin >> line; cin >> word; if (strcmp(line, word) < 0) cout << “line is alphabetically less than word”; if (strcmp(line, word) == 0) cout << “line is alphabetically equal to word”;

C-strings with char arrays… Functions: char *strstr (char *str, char *target); Returns pointer to first occurrence of target in str (or NULL) Example: char *a = “hello my friend”, *b = “my”; char *c = strstr(a, b); if (c != NULL) cout << “string b is found within string a”; *c = toupper(*c); cout << “capitalized target: “ << a;

C-string conversions #include <cstdlib> Functions: int atoi (char *str); Convert numeric digits in str into an integer float atof (char *str); Convert numeric digits in str into a float Example: char word[] = {“1234”}; int x; cin >> x; if (x < atoi(word)) cout << “x is less than word”;

C-string conversions Functions: void sprintf_s (char *str, char *format, {args} ); Convert number arguments in str according to format Format placeholders: %d place the next argument here as an int %f place next argument here as a float %8d place next argument in next 8 spaces as an int %10.2f place next argument in next 10 spaces, rounded to 2 decimal places as a float

C-string conversions Example: float x = 3.14159; int y = 123; char line[80]; sprintf_s (line, “x is: %f y is: %d”, x, y); cout << line; sprintf_s (line, “x is: %8.2f y is: %4d”, x, y);

C-strings vs. C++ string char word[30]; cin >> word; cout << “word length: “ << strlen(word); string word; cout << “word length: “ << word.length();

C-strings vs. C++ string char word[30], line[80]; cin >> line; cin >> word; strcat (line, word); cout << “complete line: “ << line; string word, line; line += word; cout << “completed line: << line;

C-strings vs. C++ string char word[30], line[80]; cin >> word; strcpy (line, word); cout << “copied word: “ << line; string word, line; line = word; cout << “copied word: << line;

C-strings vs. C++ string char word[30], line[80]; cin >> line >> word; if (strcmp(line, word) < 0) cout << “line is alphabetically less than word”; string word, line; if (line < word)

C-strings vs. C++ string char *a = “hello my friend”, *b = “my”; char *c = strstr(a, b); if (c != NULL) cout << “string b is found within string a”; string a = “hello my friend”, b = “my”; int n = a.find(b); if (n >= 0)

C-strings vs. C++ string char *a = “hello my friend”, *b = “my”; char *c = strstr(a, b); *c = toupper(*c); string a = “hello my friend”, b = “my”; int n = a.find(b); a[n] = toupper(a[n]);

C-strings vs. C++ string C++ string has over 30 additional manipulation functions! Why would anyone want to use C-strings? Using C++ string variables significantly increases the size of your program (and perhaps decreases efficiency). Historically, C-strings were used long before string was available.