#include #define PutC(c) (putc( (char) (c), stdout)) #define GetC() (getc(stdin)) #define EMPTYLOOP {} #define PutD(d) (PutC('0'+(d))) int PutV_u(unsigned int n) { unsigned int m; int i, count; /* Set m to the largest power or ten <= n */ for (m = 1; n/m >=10; m*=10) /* EMPTYLOOP */; /* Print out the digits in n one-by-one */ for (count = 0; m > 0; m /= 10) { for(i = 0; n >= m; i++) n -= m; PutD(i); count++; } return count; } int PutV_i(int n) { if (n < 0) { PutC('-'); return 1 + PutV_u(-n); } return PutV_u(n); } int PutS(char *s) { int i; for (i = 0; s[i] != '\0'; i++) PutC(s[i]); return i; } int main(void) { int i; char *TheString; char StaticString[81]; int *jp, *kp; int maxlen, c; PutS("Fun with strings\n\n"); PutS("EXAMPLE #1 - Literal Strings\n"); TheString = "A string literal"; PutS("The String is: ["); PutS(TheString); PutS("]\n\n"); /* return; */ PutS("EXAMPLE #2 - Statically Allocated Space\n"); for (i = 'A'; i < 'Z'; i++) StaticString[i-'A'] = i; StaticString[i-'A'] = '\0'; PutS("The String is: ["); PutS(StaticString); PutS("]\n\n"); /* return; */ PutS("EXAMPLE #3 - Partway into the String\n"); PutS("The String is: ["); PutS(&StaticString['N'-'A']); PutS("]\n\n"); /* return; */ PutS("EXAMPLE #4 - Data Collision\n"); /* Assigning the data pointed to by jp and kp to be within the space allocated to StaticString */ jp = &StaticString[16]; /* Array notation */ kp = StaticString + 40; /* Pointer notation */ *jp = 4242; *kp = 42; PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); /* return; */ PutS("EXAMPLE #5 - Intentional Cross-representation\n"); *jp = 'q' + 'r'*256; PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); /* return; */ PutS("Attempt #1 - Getting a string from the keyboard\n"); PutS(" Character by character until newline is read.\n"); PutS("Enter String: "); i = 0; do { StaticString[i++] = GetC(); } while (StaticString[i-1] != '\n'); PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); /* return; */ PutS("Attempt #2 - Getting a string from the keyboard\n"); PutS(" Character by character until newline is read.\n"); PutS(" Append the null terminator.\n"); PutS("Enter String: "); i = 0; do { StaticString[i++] = GetC(); } while (StaticString[i-1] != '\n'); StaticString[i] = '\0'; PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); /* return; */ PutS("Attempt #3 - Getting a string from the keyboard\n"); PutS(" Character by character until newline is read.\n"); PutS(" Append the null terminator.\n"); PutS(" Exclude the trailing newline.\n"); PutS("Enter String: "); i = 0; do { StaticString[i++] = GetC(); } while (StaticString[i-1] != '\n'); StaticString[i-1] = '\0'; PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); /* return; */ PutS("Attempt #4 - Getting a string from the keyboard\n"); PutS(" Character by character until newline is read.\n"); PutS(" Append the null terminator.\n"); PutS(" Exclude the trailing newline.\n"); PutS(" Prevent Buffer Overrun.\n"); PutS("Enter String: "); maxlen = 40; i = 0; while ( (i < maxlen) && ('\n' != (c = GetC())) ) { StaticString[i++] = c; } StaticString[i] = '\0'; PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); PutS("Attempt #4A - Getting a string from the keyboard\n"); PutS(" Character by character until newline is read.\n"); PutS(" Append the null terminator.\n"); PutS(" Exclude the trailing newline.\n"); PutS(" Prevent Buffer Overrun.\n"); PutS("Enter String: "); maxlen = 40; i = 0; while ( (i < maxlen) && ('\n' != (c = GetC())) ) { StaticString[i++] = c; } StaticString[i] = '\0'; PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); /* return; */ PutS("Attempt #5 - Getting a string from the keyboard\n"); PutS(" Character by character until newline is read.\n"); PutS(" Append the null terminator.\n"); PutS(" Exclude the trailing newline.\n"); PutS(" Prevent buffer overrun.\n"); PutS(" Leave Room for the null-terminator.\n"); PutS(" Clear input buffer of remaining characters.\n"); PutS("Enter String: "); *kp = 28000; maxlen = 40; i = 0; while ( (i < (maxlen-1)) && ('\n' != (c = GetC())) ) { StaticString[i++] = c; } StaticString[i] = '\0'; while ( '\n' != c ) c = GetC(); PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); PutS("Attempt #5A - Getting a string from the keyboard\n"); PutS(" Character by character until newline is read.\n"); PutS(" Append the null terminator.\n"); PutS(" Exclude the trailing newline.\n"); PutS(" Prevent buffer overrun.\n"); PutS(" Leave Room for the null-terminator.\n"); PutS(" Clear input buffer of remaining characters.\n"); PutS("Enter String: "); *kp = -32000; maxlen = 40; i = 0; while ( (i < (maxlen-1)) && ('\n' != (c = GetC())) ) { StaticString[i++] = c; } StaticString[i] = '\0'; while ( '\n' != c ) c = GetC(); PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); /* return; */ PutS("Attempt #6 - Getting a string from the keyboard\n"); PutS(" Character by character until newline is read.\n"); PutS(" Append the null terminator.\n"); PutS(" (scratch) Exclude the trailing newline.\n"); PutS(" Prevent buffer overrun.\n"); PutS(" Leave Room for the null-terminator.\n"); PutS(" Clear input buffer of remaining characters.\n"); PutS(" Include newline character to mark success.\n"); PutS("Enter String: "); maxlen = 40; i = 0; while ( (i < (maxlen-1)) && ('\n' != (c = GetC())) ) { StaticString[i++] = c; } if ( i < (maxlen-1) ) StaticString[i++] = c; StaticString[i] = '\0'; while ( '\n' != c ) c = GetC(); PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); PutS("Attempt #6A - Getting a string from the keyboard\n"); PutS(" Character by character until newline is read.\n"); PutS(" Append the null terminator.\n"); PutS(" (scratch) Exclude the trailing newline.\n"); PutS(" Prevent buffer overrun.\n"); PutS(" Leave Room for the null-terminator.\n"); PutS(" Clear input buffer of remaining characters.\n"); PutS(" Include newline character to mark success.\n"); PutS("Enter String: "); maxlen = 40; i = 0; while ( (i < (maxlen-1)) && ('\n' != (c = GetC())) ) { StaticString[i++] = c; } if ( i < (maxlen-1) ) StaticString[i++] = c; StaticString[i] = '\0'; while ( '\n' != c ) c = GetC(); PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); /* return; */ PutS("Attempt #7 - Getting a string from the keyboard\n"); PutS(" Character by character until newline is read.\n"); PutS(" Append the null terminator.\n"); PutS(" Prevent buffer overrun.\n"); PutS(" Leave Room for the null-terminator.\n"); PutS(" Clear input buffer of remaining characters.\n"); PutS(" Exclude newline character.\n"); PutS(" Count the number of characters discarded.\n"); PutS("Enter String: "); maxlen = 40; i = 0; while ( (i < (maxlen-1)) && ('\n' != (c = GetC())) ) { StaticString[i++] = c; } StaticString[i] = '\0'; for ( *jp = 0; '\n' != c; (*jp)++ ) /* Pointer Notation */ c = GetC(); PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); PutS("Attempt #7A - Getting a string from the keyboard\n"); PutS(" Character by character until newline is read.\n"); PutS(" Append the null terminator.\n"); PutS(" Prevent buffer overrun.\n"); PutS(" Leave Room for the null-terminator.\n"); PutS(" Clear input buffer of remaining characters.\n"); PutS(" Exclude newline character.\n"); PutS(" Count the number of characters discarded.\n"); PutS("Enter String: "); maxlen = 40; i = 0; while ( (i < (maxlen-1)) && ('\n' != (c = GetC())) ) { StaticString[i++] = c; } StaticString[i] = '\0'; for ( jp[0] = 0; '\n' != c; jp[0]++ ) /* Array Notation */ c = GetC(); PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); /* return; */ PutS("Attempt #7B - Getting a string from the keyboard\n"); PutS(" Character by character until newline is read.\n"); PutS(" Append the null terminator.\n"); PutS(" Prevent buffer overrun.\n"); PutS(" Leave Room for the null-terminator.\n"); PutS(" Clear input buffer of remaining characters.\n"); PutS(" Exclude newline character.\n"); PutS(" Count the number of characters discarded.\n"); PutS("Enter String: "); maxlen = 40; i = 0; while ( (i < (maxlen-1)) && ('\n' != (c = GetC())) ) { StaticString[i++] = c; } StaticString[i] = '\0'; for ( *kp = 0; '\n' != c; (*kp)++ ) /* Pointer Notation */ c = GetC(); PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); PutS("Attempt #7C - Getting a string from the keyboard\n"); PutS(" Character by character until newline is read.\n"); PutS(" Append the null terminator.\n"); PutS(" Prevent buffer overrun.\n"); PutS(" Leave Room for the null-terminator.\n"); PutS(" Clear input buffer of remaining characters.\n"); PutS(" Exclude newline character.\n"); PutS(" Count the number of characters discarded.\n"); PutS("Enter String: "); maxlen = 40; i = 0; while ( (i < (maxlen-1)) && ('\n' != (c = GetC())) ) { StaticString[i++] = c; } StaticString[i] = '\0'; for ( kp[0] = 0; '\n' != c; kp[0]++ ) /* Array Notation */ c = GetC(); PutS("The String is: ["); PutS(StaticString); PutS("]\n"); PutS("The value pointed to by jp: <"); PutV_i(*jp); PutS(">\n"); PutS("The value pointed to by kp: <"); PutV_i(*kp); PutS(">\n\n"); /* return; */ return 0; }