190 likes | 416 Views
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);
E N D
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); cout << line;
C-strings vs. C++ string char word[30]; cin >> word; cout << “word length: “ << strlen(word); string word; cin >> 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; cin >> line; cin >> word; 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; cin >> word; 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; cin >> line >> word; if (line < word) cout << “line is alphabetically less than 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) cout << “string b is found within string a”;
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.