Strings in C - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Strings in C

Description:

Strings in C++ Chapter 4 p 125 - 131 * * The string Class The C++ Standard Library includes a string class. #include * C++ Strings C++ strings are objects. – PowerPoint PPT presentation

Number of Views:141
Avg rating:3.0/5.0
Slides: 29
Provided by: RollinS5
Learn more at: https://www.csee.usf.edu
Category:
Tags: circles | class | strings

less

Transcript and Presenter's Notes

Title: Strings in C


1
Strings in C
  • Chapter 4
  • p 125 - 131

2
The string Class
  • The C Standard Library includes a string class.
  • include ltstringgt

3
C Strings
  • C strings are objects.
  • NOT arrays
  • Assignment works
  • str1 str2
  • Concatenation
  • str3 str1 str2
  • str1 str2

4
Class string
  • Class methods handle memory allocation for the
    string.
  • Memory allocation is automatically increased as
    needed.
  • No problems with array bounds overrun.

5
String Methods
  • Methods and operators of class string replace C
    String Library functions.
  • Example
  • str1.size() returns length of str1
  • The C comparison operators are defined for
    class string. You can write
  • if (str1 lt str2)
  • if (str1 str2)
  • etc.

6
String Operators
  • The C comparison operators are defined for
    class string. You can write
  • if (str1 lt str2)
  • if (str1 str2)
  • etc.
  • The operator is defined as concatenation.
  • You can write
  • str3 str1 str2
  • str1 str2

7
Using C Style Strings in C
  • String literals are C style strings
  • Array of char
  • But we can use them with C strings.
  • C operators are overloaded to take C styles
    strings as operands.
  • Instantiation
  • string str1 "abcdef"
  • Assignment
  • str1 "abcdef"
  • Concatenation
  • str2 str1 "ghijkl"
  • str1 "ghijkl"

8
String I/O
  • cin and cout work for strings.
  • cout ltlt str1 ltlt endl
  • cin gtgt str2 Reads one word
  • getline(cin, str3) Reads entire line

9
Example
  • Create a new empty C console application
    project.
  • string_demo
  • Add main.cpp
  • We will look at several examples

10
Word at a Time Input
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • int main()
  • string str1
  • while (true)
  • cout ltlt "Enter a string "
  • cin gtgt str1
  • cout ltlt "You entered " ltlt str1
  • cout ltlt endl

11
Word at a Time Input
12
Line at a Time Input
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • int main()
  • string str1
  • while (true)
  • cout ltlt "Enter a string "
  • getline(cin,str1)
  • cout ltlt "You entered " ltlt str1
  • cout ltlt endl

13
Line at a Time Input
14
Comparison
  • int main()
  • string str1
  • string str2
  • while (true)
  • cout ltlt "Enter a string "
  • getline(cin,str1)
  • cout ltlt "You entered " ltlt str1
  • cout ltlt endl
  • cout ltlt "Enter another string "
  • getline(cin, str2)
  • if (str1 lt str2)
  • cout ltlt """" str1 """ is less
    than "
  • """" str2 """" ltlt endl

15
Using the Less Than Operator
16
Using the string Class
  • Let's revise class Circle to use a string for the
    name member.
  • Download most recent project
  • http//www.cse.usf.edu/turnerr/Object_Oriented_De
    sign/Downloads/2011_02_02_Operator_Overloading/
  • File Circle_Sort2.zip
  • Copy Circle.h and Circle.cpp into project
    directory.
  • Add to project.

17
Circle.h
  • ifndef CIRCLE_H
  • define CIRCLE_H
  • include ltiostreamgt
  • using namespace std
  • class Circle
  • public
  • static const int Max_Name 20
  • private
  • char nameMax_Name1
  • double radius
  • public
  • Circle(const char Name, double Radius)
  • Circle()
  • double Radius() const return radius

18
Circle.cpp
  • define _CRT_SECURE_NO_WARNINGS
  • include ltstring.hgt
  • include ltiostreamgt
  • include "Circle.h"
  • using namespace std
  • CircleCircle(const char Name, double Radius)
  • strncpy(name, Name, Max_Name)
  • nameMax_Name 0 // Be sure there is a
    null terminator
  • radius Radius

19
Circle.cpp
  • double CircleArea() const
  • return 3.141592 radius radius
  • void CircleDisplay() const
  • cout ltlt name ltlt " is a Circle of radius "
  • ltlt radius ltlt stdendl
  • ...
  • ostream operatorltlt(ostream os, const Circle c)
  • os ltlt c.Name() ltlt " Radius " ltlt c.Radius()
  • return os

20
Circles_Test.cpp
  • define _CRT_SECURE_NO_WARNINGS
  • include ltiostreamgt
  • include ltstringgt
  • include "Circle.h"
  • using namespace std
  • Circle Create_Circle()
  • char nameCircleMax_Name1
  • double radius
  • cout ltlt "Name? "
  • cin gtgt name
  • cout ltlt "Radius? "
  • cin gtgt radius
  • return new Circle(name, radius)

21
Circles_Test.cpp
  • int main(void)
  • Circle c1 Create_Circle()
  • cout ltlt "Area of circle " ltlt c1-gtName()
  • ltlt " is " ltlt c1-gtArea() ltlt endl
  • cin.get() // Keep the window open.
  • cin.get()
  • return 0
  • Build and run.

22
Program Running
23
Using String Class
  • Now let's modify class Circle to use a C String
    for name.

24
Changes to Circle.h
  • pragma once
  • include ltstringgt
  • using namespace std
  • class Circle
  • //public
  • // static const int Max_Name 10
  • private
  • string name
  • double radius
  • public
  • // Constructor
  • Circle(const string Name, double Radius)
  • // Return the area of this circle.
  • double Area()

25
New Constructor
  • define _CRT_SECURE_NO_WARNINGS
  • include ltstring.hgt
  • include ltiostreamgt
  • include "Circle.h"
  • using namespace std
  • CircleCircle(const string Name, double Radius)
  • name Name
  • radius Radius

26
New Create_Circle Function
  • define _CRT_SECURE_NO_WARNINGS
  • include ltiostreamgt
  • include "Circle.h"
  • using namespace std
  • Circle Create_Circle()
  • string name
  • double radius
  • cout ltlt "Name? "
  • cin gtgt name
  • cout ltlt "Radius? "
  • cin gtgt radius
  • return new Circle(name, radius)

This still works!
27
Main is Unchanged
28
Summary
  • The string class in C permits us to treat
    strings as objects rather than as arrays.
  • MAJOR improvement over C style strings.
  • End of Presentation
Write a Comment
User Comments (0)
About PowerShow.com