char greeting[20] = "Hello, World"; cout << greeting; // prints "Hello, World" char lastname[20]; cin >> lastname; // reads a string into the array 'lastname' // adds the null character automatically
char* get(char str[], int length, char delimiter = '\n'); char* getline(char str[], int length, char delimiter = '\n');
char ch; ch = cin.get(); // extracts one character, returns it cin.get(ch); // extracts one character, stores in ch
char buffer[80]; cin >> buffer; // reads one word into buffer cin.get(buffer, 80, ','); // reads up to the first comma, stores in buffer cin.getline(buffer, 80); // reads an entire line (up to newline)
char greeting[15], name[10], other[20]; cin.getline(greeting,15); // gets input into the greeting array cin.get(name,10,'.'); // gets input into the name array cin.getline(other,20); // gets input into the other array
Suppose that the data on the input stream (i.e. typed onto the keyboard, for instance) is:
Hello, World Joe Smith. He says hello.At this point, the contents of each string are:
greeting: "Hello, World" name: "Joe Smith" other: ". He says hello."Here's an example illustrating some different calls that read strings
#include <cstring>
This string library contains many useful string manipulation functions. These are all for use with C-style strings. A few of the more commonly used ones are mentioned here. (The textbook contains more detail in chapter 10)
int strlen(const char str[]);
char phrase[30] = "Hello, World"; cout << strlen("Greetings, Earthling!"); // prints 21 int length = strlen(phrase); // stores 12
char* strcpy(char str1[], const char str2[]); // copies str2 into str 1
char buffer[80], firstname[30], lastname[30] = "Smith"; strcpy(firstname, "Billy Joe Bob"); // copies name into firstname array strcpy(buffer, lastname); // copies "Smith" into buffer array cout << firstname; // prints "Billy Joe Bob" cout << buffer; // prints "Smith"
char* strcat(char str1[], const char str2[]); // concatenates str2 onto the end of str1
char buffer[80] = "Dog"; char word[] = "food"; strcat(buffer, word); // buffer is now "Dogfood" strcat(buffer, " breath"); // buffer is now "Dogfood breath"
int strcmp(const char str1[], const char str2[]); // returns: a negative number, if str1 comes before str2 // a positive number, if str2 comes before str1 // 0 , if they are equal // // Note: Lexicographic order is by ascii codes. It's NOT the same // as alphabetic order.
char word1[30] = "apple"; char word2[30] = "apply"; if (strcmp(word1, word2) != 0) cout << "The words are different\n"; strcmp(word1, word2) // returns a negative, means word1 comes first strcmp(word1, "apple") // returns a 0. strings are the same strcmp("apple", "Zebra") // returns a positive. "Zebra" comes first! // (all uppercase before lowercase in ascii)
char buffer[80]; char word[11] = "applesauce"; char bigword[] = "antidisestablishmentarianism"; strncpy(buffer, word, 5); // buffer now stores "apple" strncat(buffer, " piecemeal", 4); // buffer now stores "apple pie" strncmp(buffer, "apple", 5); // returns 0, as first 5 characters // of the strings are equal strncpy(word, bigword, 10); // word is now "antidisest" // word only had 11 slots!These functions can be used to help do safer string operations. The extra parameter can be included to guarantee that array boundaries are not exceeded, as seen in the last strncpy example