/* This program demonstrates * 1. c-style strings * 2. The input stream and how it interacts with cin * * * A string is the word programmers use to represent text. * We can store a single character in a char variable. To store many characters (text), * we need multiple char variables. The quick solution to this is to use an * array of characters. An array of chars is called a C-String. * Since C-Strings are arrays, every concept that applies to arrays also applies * to C-Strings. So, a C-String has elements in contiguous memory locations, * we can access individual elements using the index, the index starts at 0. * * There are 4 major points of difference between a regular numeric array we are * familiar with and a C-String * 1. End-of-String: If we have a regular numeric array, and we have fewer values * than spots allocated, then the rest of the spots are filled out with 0's (if * we initialize the array) or with junk values (if we do not initialize). In * case of C-Strings, the spot after the last value is assigned the end-of-string * character. The end-of-string character is '\0'. C++ considers all the characters * from position 0 until the end-of-string to be the string. * * 2. Direct initialization: If we want to initialize a numeric array, we have * to use set notation upon declaration. However, for strings we can either use * the set notation or use a double quoted string literal. * * 3. Direct printing: It is not possible to directly print a numeric array. * If we tried, we would just get the starting address of the array. However, * we can directly print a C-String (put it in the cout without using a loop). * * 4. Direct reading: We cannot directly read in the entire numeric array. However, * we can use a cin statement to read in the C-string (instead of using a loop). * * A Note on streams: * cin reads from the input stream (stdin), which sends data from the * keyboard to our program. To save time and make this process more efficient * the input stream will send in what we typed only when we press the Enter key. * So, multiple pieces of information could be sent to the input stream (cin) * However, cin in whitespace delimited. It will read from the first character * remaining on the stream until the first whitespace character. If any charatcer * does not match wha cin is expecting, it will crash. * When we use cin with a string just like we use it with numbers, it will * read until it encounters the first whitespace character. So, it is capable of * reading only one "word" */ #include #include // for query functions #include // for string length using namespace std; int main() { //Declare a C-string and initialize it with set notation char str[100] = {'H', 'e', 'l', 'l', 'o', '\0'}; //Directly print a string cout<<"The string is "<>str; cout<<"You entered "<>st1; //We can also print individual characters // C++ does not check for bounds, we have to do it ourselves. for(int i=0; i<100 ; i++) cout<>x; cout<<"Enter a string: "; cin>>st; cout<<"Enter another number: "; cin>>y; cout<<"x: "<