1 / 30

CIS 4930 Application Development Using C++ Dr. Kun Suk Kim CISE Department, University of Florida

CIS 4930 Application Development Using C++ Dr. Kun Suk Kim CISE Department, University of Florida. Strings. Character-Manipulating Functions in <cctype>. int toupper(int c) Return type is int Example: char c = toupper(‘a’); cout << c << “ “ << toupper('a') << endl; Output: A 65

bailey
Download Presentation

CIS 4930 Application Development Using C++ Dr. Kun Suk Kim CISE Department, University of Florida

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS 4930Application Development Using C++Dr. Kun Suk KimCISE Department, University of Florida Strings

  2. Character-Manipulating Functions in <cctype> • int toupper(int c) • Return type is int • Example: char c = toupper(‘a’); cout << c << “ “ << toupper('a') << endl; Output: A 65 • int tolower(int c)

  3. Character-Manipulating Functions in <cctype> • bool isupper(int c) • Returns true for uppercase letter; otherwise, returns false • char c = ‘A’; if (isupper(c)) cout << c << “is uppercase.”; Output: A is uppercase. • bool islower(int c)

  4. Character-Manipulating Functions in <cctype> • bool isalpha(int c) • Tests for any character for which isupper() or islower() is true • bool isdigit(int c) • tests for any decimal-digit character • bool isalnum(int c) • tests for any character for which isalpha() or isdigit() is true (letter or digit) • bool isspace(int c) • tests for any standard white-space characters

  5. Character-Manipulating Functions in <cctype> • bool isprint(int c) • tests for any character for which ispunct(), isupper(), islower(), isdigit(), and the space character (“ ") is true • bool iscntrl(int c) • tests for any "control character'' as defined by the character set • bool ispunct(int c) • tests for any printing character which is neither a space (“ ") nor a character for which isalnum() or iscntrl() is true (i.e., punctuation)

  6. Standard Class string • Defined in library:#include <string>using namespace std; • String variables and expressions • Treated much like simple types • Can assign, compare, add:string s1, s2, s3;s3 = s1 + s2; //Concatenations3 = “Hello Mom!” //Assignment • Note c-string “Hello Mom!” automaticallyconverted to string type!

  7. Type basic_string • Type basic_string<> is defined in <string> namespace std { template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > class basic_string; }

  8. Type basic_string • First parameter is data type of a single character • Second parameter is a traits class • Provides all core operations for the characters of the string class • How to copy or to compare characters • Third parameter defines the memory model that is used by the string class • Default memory model allocator (Ch 15)

  9. Type string and wstring • string is predefined specialization of class basic_string<> for characters of type char • wstring is predefined specialization of class basic_string<> for characters of type wchar_t • Wider character sets (Unicode or some Asian character sets) • namespace std { typedef basic_string<char> string; typedef basic_string<wchar_t> wstring; }

  10. Character Traits • Different representation of character sets • Variation for processing of strings and I/O • Example: • Value used to represent “end-of-file” • Details of comparing charaters • Specialization for char is usually implemented by using the global string functions of C • Defined in <cstring> or <string.h>

  11. Character Traits Members namespace std { template<> struct char_traits<char> { // type definitions: typedef char char_type; typedef int int_type; … // functions: static void assign(char& c1, const char& c2) { c1 = c2; } static bool eq(const char& c1, const char& c2) { return c1 == c2; }

  12. Character Traits Members static bool lt(const char& c1, const char& c2) { return c1 < c2; } static size_t length(const char* s) { return strlen(s); } static int compare(const char* s1, const char* s2, size_t n) { return memcmp(s1, s2, n); } static char* copy(char* s1, const char* s2, size_t n) { return (char*)memcpy(s1, s2, n); }

  13. Character Traits Members static char* move(char* s1, const char* s2, size_t n) { return (char*)memmove(s1, s2, n); } static char* assign(char* s, size_t n, char c) { return (char*)memset(s, c, n); } static const char* find(const char* s,size_t n, const char& c) { return (const char*)memchr(s, c, n); } static int eof() { return EOF; }

  14. Character Traits Members static int to_int_type(const char& c) { return (int)(unsigned char) c; } static char to_char_type(const int& i) { return (char)i; } static int not_eof(const int& i) { return i != EOF? i : !EOF; // EOF: -1, !EOF: false } static bool eq_int_type(const int& i1, const int& i2) { return i1 == i2; } }; } // namespace std

  15. User-Defined Traits Class #ifndef ICSTRING_HPP #define ICSTRING_HPP #include <string> #include <iostream> #include <cctype> // strings behave in a case-insensitive way struct ignorecase_traits : public std::char_traits<char> { static bool eq(const char& c1, const char& c2) { return toupper(c1)==toupper(c2); } static bool lt(const char& c1, const char& c2) { return toupper(c1)<toupper(c2); }

  16. User-Defined Traits Class static int compare(const char* s1, const char* s2, size_t n) { for (size_t i=0; i<n; ++i) if (!eq(s1[i],s2[i])) return lt(s1[i],s2[i])?-1:1; return 0; } static const char* find(const char* s, size_t n, const char& c) { for (size_t i=0; i<n; ++i) if (eq(s[i],c)) return &(s[i]); return 0; } };

  17. User-Defined Traits Class // define a special type for such strings typedef std::basic_string<char,ignorecase_traits> icstring; // define an output operator because the traits type is different than // that for std::ostream inlinestd::ostream& operator << (std::ostream& strm, const icstring& s) { return strm << std::string(s.data(),s.length()); } #endif // ICSTRING_HPP

  18. User-Defined Traits Class #include "icstring.hpp“ int main(){ using namespace std; icstring s1("hallo"), s2("otto"); icstring s3("hALLo"); cout << std::boolalpha; cout << s1 << " == " << s2 << " : " << (s1==s2) << endl; cout << s1 << " == " << s3 << " : " << (s1==s3) << endl; icstring::size_type idx = s1.find("All"); if (idx != icstring::npos) cout << "index of \"All\" in \"" << s1 << "\": " << idx << endl; else cout << "\"All\" not found in \"" << s1 << endl; } hallo == otto : false hallo == hALLo : true index of "All" in "hallo": 1

  19. Value npos • class basic_string { public: typedef typename Allocator::size_type size_type; … static const size_type npos = -1; }; • size_type must be an unsigned integral type • Default allocator, allocator, uses type size_t as size_type • -1 is converted into an unsigned integral type • npos is the maximum unsigned value of its type • The exact value depends on the exact definition of type size_type

  20. Value npos • If idx has the value –1 and idx (say unsigned short) and string::npos (say unsigned long) have different type, • idx == std::string::npos might yield false.

  21. I/O with Class string • Just like other types! • string s1, s2;cin >> s1;cin >> s2; • Results:User types in:May the hair on your toes grow long and curly! • Extraction still ignores whitespace:s1 receives value “May”s2 receives value “the”

  22. getline() with Class string • For complete lines:string line;cout << “Enter a line of input: “;getline(cin, line);cout << line << “END OF OUTPUT”; • Dialogue produced:Enter a line of input: Do be do to you!Do be do to you!END OF INPUT • Similar to c-string’s usage of getline()

  23. Other getline() Versions • Can specify ‘delimiter’ character:string line;cout << “Enter input: “;getline(cin, line, ‘?’); • Receives input until ‘?’ encountered • getline() actually returns reference • string s1, s2;getline(cin, s1) >> s2; • Results in: (cin) >> s2;

  24. Class string Processing • Same operations available as c-strings • And more! • Over 100 members of standard string class

  25. Some string Member Functions • length(), size() • Returns the number of characters • at(i), [] • Access a character • Returns reference to char at position i • ==, !=, <, <=, >, >=, compare() • Compare strings • + • Concatenates strings

  26. Some string Member Functions • data() • Returns the value as character array • substr() • Returns a certain substring • c_str() • Returns the value as C-string • copy() • Copies or writes the contents to a C-string

  27. string Member Functions as Container Interface • max_size() • Returns the maximum possible number of characters • empty() • Returns whether the string is empty • capacity() • Returns the number of characters that can be held without reallocation • Find functions • Search for a certain substring or character

  28. string Member Functions as Container Interface • begin(), end() • Provide normal iterator support • rbegin(), rend() • Provide reverse iterator support • insert() • Inserts characters • erase() • Deletes characters • clear() • Removes all characters (make it empty)

  29. C-string and string Object Conversions • Automatic type conversions • From c-string to string object:char aCString[] = “My C-string”;string stringVar;stringVar = aCstring; • Perfectly legal and appropriate! • aCString = stringVar; • ILLEGAL! • Cannot auto-convert to c-string • Must use explicit conversion:strcpy(aCString, stringVar.c_str());

  30. Summary • C-string variable is ‘array of characters’ • With addition of null character, ‘\0’ • C-strings act like arrays • Cannot assign, compare like simple variables • Libraries <cctype> & <string> have usefulmanipulating functions • cin.get() reads next single character • getline() versions allow full line reading • Class string objects are better-behavedthan c-strings

More Related