String 1. C-Style String A C-Style string is any null-terminated byte string, where this is a sequence of nonzero bytes followed by a byte with zero (0) value (the terminating null character).
The terminating null character is represented as the character literal ’\0’; The length of an NTBS is the number of elements that precede the terminating null character. An empty NTBS has a length of zero. The size of an NTBS is the size of the entire array, including the terminating null character. A single quotes (') are used to identify character literals. A double quotes ('') are used to identify string literals. String literals are stored in your program image, usually in a read-only section (.data), 1.1. Create a String // *1. Ptr char* strMessagePtr= "abc"; // sizeof(strMessage) = 32 or 64 (ptr) strMessagePtr[0] = 1; // error, ptr to const // *2. Arrays char strMessage[] = "abc"; // sizeof(strMessage ) = 4 strMessage[1] = 'a'; // OK For strMessage, the memory for the array is allocated on the stack at runtime. The compiler initializes it from the string literal. At runtime, the program memory copies the string literal into the array For strMessagePtr, only the address of the string literal is held on the stack, and there is no copying of string literal. 1.2. Characters Null: \0, 0x00, NULL Carriage Return And New Line: \r\n Case switching: 'A' ^ ' ' & 'a' ^ ' ' Special: \\, Escape sequences: Name Symbol Meaning Alert \a Makes an alert, such as a beep Backspace \b Moves the cursor back one space Formfeed \f Moves the cursor to next logical page Newline \n Moves cursor to next line Carriage return \r Moves cursor to beginning of line Horizontal tab \t Prints a horizontal tab Vertical tab \v Prints a vertical tab Single quote \' Prints a single quote Double quote \" Prints a double quote Backslash \\ Prints a backslash Question mark \? Prints a question mark (no longer relevant) Octal number \{number} Translates into char represented by octal Hex number \x{number} Translates into char represented by hex number 1.3. C String Libraries Copying strings : strcpy, strncpy Concatenating strings: strcat, strncat Comparing strings: strcmp, strncmp Parsing strings: strtok, strcspn Length: strlen Examples: #include <stdio.h> #include <string.h> int main() { // ------------------------------- // 1. Copying strings // ------------------------------- char src[] = "Hello"; char dst[20]; strcpy(dst, src); // copy full string // dst = "Hello" strncpy(dst, "World", 3); // copy only 3 chars dst[3] = '\0'; // ensure null-termination // dst = "Wor" // ------------------------------- // 2. Concatenating strings // ------------------------------- char text[20] = "Hi"; strcat(text, " there"); // append full string // text = "Hi there" strncat(text, "!!!", 2); // append only 2 chars // text = "Hi there!!" // ------------------------------- // 3. Comparing strings // ------------------------------- int r1 = strcmp("abc", "abc"); // r1 = 0 (equal) int r2 = strcmp("abc", "abd"); // r2 < 0 (abc < abd) int r3 = strncmp("abcdef", "abcxyz", 3); // r3 = 0 (first 3 chars equal) // ------------------------------- // 4. Parsing strings // ------------------------------- char line[] = "A,B,C"; char* token = strtok(line, ","); // first token: "A" while (token != NULL) { printf("token: %s\n", token); token = strtok(NULL, ","); } // strcspn: find first occurrence of any chars in reject set char sample[] = "hello123world"; size_t pos = strcspn(sample, "0123456789"); // pos = 5 (first digit is at index 5) // ------------------------------- // 5. Length // ------------------------------- size_t len = strlen("abc"); // len = 3 return 0; } 1.4. String/Numbers Conversion Integer to String: itoa() (non-standard) String to Double: atof() String to Double (with error checking): strtod() String to Long (with base + error checking): strtol() e.g. #include <stdio.h> #include <stdlib.h> // atof, strtod, strtol #include <string.h> // itoa (non-standard on some compilers) int main() { // ------------------------------- // 1. Integer → String (itoa) // ------------------------------- char buf[32]; itoa(1234, buf, 10); // convert integer to string in base 10 // buf = "1234" itoa(255, buf, 16); // convert to hex // buf = "ff" // ------------------------------- // 2. String → Double (atof) // ------------------------------- double d1 = atof("3.14159"); // d1 = 3.14159 double d2 = atof("12.5xyz"); // d2 = 12.5 (atof stops at non-numeric chars) // ------------------------------- // 3. String → Double (strtod) // ------------------------------- char* end; double d3 = strtod("45.67abc", &end); // d3 = 45.67 // end -> "abc" // ------------------------------- // 4. String → Long (strtol) // ------------------------------- long v1 = strtol("1234", NULL, 10); // v1 = 1234 (decimal) long v2 = strtol("FF", NULL, 16); // v2 = 255 (hex → decimal) char* end2; long v3 = strtol("100xyz", &end2, 10); // v3 = 100 // end2 -> "xyz" return 0; } 2. C++ String Strings are objects that represent sequences of characters.
...