1 / 19

CS 1400

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);

sloan
Download Presentation

CS 1400

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. CS 1400 27 Nov 2006 C-strings

  2. 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);

  3. 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);

  4. 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)…

  5. 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);

  6. 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;

  7. 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;

  8. 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”;

  9. 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;

  10. 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”;

  11. 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

  12. 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); cout << line;

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

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

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

  16. 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; cin >> line >> word; if (line < word) cout << “line is alphabetically less than word”;

  17. 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) cout << “string b is found within string a”;

  18. 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]);

  19. 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.

More Related