//* Introductory example to C-style strings *
#include <iostream> #include <cstring> //Note: <string> is a different library using namespace std; int main() { char name[30]; // note: max 29 characters stored because of an extra terminating '\0' cout << "What is your name, please? "; //cin >> name; // reads only one word to a space or new line after it // this line above will read one word no matter how long it is, unsafe!!! cin.getline(name,30); // reads a line of text including spaces // this line above will read only up to 29 characters of the line name[0]=toupper(name[0]); if (strcmp(name, "Alexander")==0) { cout << "Welcome back my master programmer!" << endl; } else { cout << "Hello " << name << ", it is nice to meet you." << endl; if ( strlen(name)>=10 ) { // #of chars >=10 char * location = strchr(name,' '); if ( location==0 ) { // no space at all (zero pointer location) cout << name << " is a very nice but also a very long name." << endl; } else { // non zero location is another entry point to the same string ++location; location[0]=toupper(location[0]); // note: we are also changing name at the same time cout << "So your middle name is " << location << "?" << endl; } } } cout << "Good Bye " << name << "!" << endl; return(0); } // Other important functions to remember: // strcpy ( copyto, copyfrom) // strcat ( appendto, copyfrom) // strcatchr( appendto, onechr) // strtrunc ( string, atposition) // Searching functions // strstr ( string, searchforstr) // strchr ( string, searchforchr)