#include using namespace std; /******************** * * C-style strings * ********************/ // In C strings are char arrays // note that the size of the array is 6 but the number of characters appears to be 5 // that is because the last character is a Null character '\0' when you assign values in "" // to print strings in C, include stdio.h #include // for printf(). Also would do /* int main() { char str[6] = "ABCDE"; printf("str = %s \n", str); // printf() is cout of C. %s is a placeholder for a string char str2[200]; scanf("%s", str2); //scanf() is cin of C printf("You entered: %s \n", str2); int i = 67; printf("i = %i \n", i ); printf("i = %d \n", i ); printf("i = %c \n", i ); } */ // other placeholders for printf: %c for char, %i and %d for int, %f for float and double... // string library #include // Also // strlen( str ) - returns the number of characters without NULL character // strcmp( str1, str2 ) - returns the difference between 2 strings. 0 if they are same // strcpy( dest, src ) - copies src into dest // strncpy( dest, src, n ) - copies first n chars from src into dest // search functions // strchr ( str, char ) - returns a pointer to the first occurance of char in str // strstr ( str, searchstr ) - returns a pointer to the first occurance of searchstr in str // ex: /* int main() { char s1[6] = "ABCDE"; char s2[6] = "gf"; int len = strlen(s1); printf("length of s1: %i \n", len ); printf("length of s1: %d \n", strlen(s1)); printf("length of %s is %d \n", s1, strlen(s1)); // s1 = s2; // ILLEGAL // s1 = "fgsdf"; // ILLEGAL strncpy( s1, s2, strlen(s2) ); printf("s1: %s \n", s1 ); strcpy( s2, s1 ) ; printf("s2: %s \n", s1 ); char s3[6] = "gfCDF"; printf("comparing s2 and s3: %d \n", strcmp(s2,s3)); return 0; } */ /*************** * * File I/O * ***************/ // related data // sequential access - imagine a cursor sits at a position in the file. last position - eof /* 3 steps involved: 1) open file ( reading || writing ) 2) read from || write to the file 3) close file */ // include header - contains ifstream (input file) and ofstream (output file) classes #include // reading example /* int main(){ ifstream inFile; inFile.open("myfile.dat", ios::in); // ios::in is the mode to open the file. in is for reading // ifstream inFile("myfile.dat", ios::in); // check if the file opened properly if ( !inFile ) { cout << "Error opening file!" << endl ; return 1; } // read first 2 numbers int x, y; inFile >> x; inFile >> y; // OR inFile >> x >> y; // read the rest while( !inFile.eof() ) { inFile >> x ; cout << "x = " << x << endl ; } inFile.close(); return 0; } */ // writing example /* int main(){ ofstream outFile; outFile.open("../outfile.dat", ios::out); // out - open the file writing, app - to append // check if the file opened properly if ( !outFile ) { cout << "Error opening file!" << endl ; return 1; } int x = 3; outFile << "Hello, world! \n"; outFile << "x = " << x << endl ; outFile.close(); return 0; } */ /*********************************** * * Character handling w/ ctype.h * ***********************************/ // character handling library of C #include // some useful functions: // isdigit(char) - returns true if char is 0..9 // isalpha(char) - returns true if char is A..Z or a..z // isalnum(char) - returns true if char is a digit or alphabet // islower(char) - returns true if a..z // isupper(char) - returns true if A..Z // How do these functions work? -> ASCII table // 0 - 9 in ASCII 48 - 57 // A - Z in ASCII 65 - 90 // a - z in ASCII 97 - 122 // OR If you want to check the ASCII value of a character: /* int main(){ char c = 'a'; int ascii_val = c; cout << "ascii value of '" << c << "' is = " << ascii_val << endl; } */ // ex1: write a program that counts the number of upper case, lower case and numeric characters // in a user provided string #include #include #include /* int main() { // read input from user printf("Enter a string: "); char str[100]; scanf("%s", str); // count #digits, #upper, #lower int d = 0, u = 0, l =0; for( int i=0; i < strlen(str); i++ ){ if ( isdigit(str[i]) ) d++; if ( isupper(str[i]) ) u++; if ( islower(str[i]) ) l++; } // display results printf("#digits = %i, #upper case letters = %i, #lower case letters = %i \n",d,u,l); return 0; } */ // ex2: modify the above program to write upper case letters in a file called "upper.dat" // and lower case letters in a file called "lower.dat" #include #include #include #include int main() { printf("Enter string: "); char str[100]; scanf("%s", str); ofstream outUpper, outLower; outUpper.open("upper.dat", ios::out); outLower.open("lower.dat", ios::out); for(int i=0; i< strlen(str); i++){ if ( isupper(str[i]) ){ outUpper << str[i] ; } if ( islower(str[i]) ){ outLower << str[i] ; } } outUpper.close(); outLower.close(); return 0; }