Strings (c and c++)
- Strings are an array of characters
- * they are accessed using the address of the first character (item) in the array
- A String is an array of characters ending in a null character
\0
char color[] = βblueβ;
// 5 Char array continaing 'b', 'l', 'u', 'e', '\0'
char *colorPtr = βblueβ;
// pointer to memory location constinaing 'blue'
- donβt forget to size arrays to hold the null character if explicitly defining array length
C++ String Read functions
- Collect unlimited Characters until space, tab or EOL
cin >> word;
- Collect up to 19 characters or end on space, tab or EOL
cin >> setw(20) >> word;
- Collect full line of text, in the example up to 80 characters ending in a new line (
\n)
char sentence[80];
cin.getline(sentence, 80, β\nβ);
Standard C Libraries
stdio.hstring.hβ String Librarystddef.hβ contains standard type definitionsstdlib
Printing
int printf(const char *format, ...)
%[flags][width][.precision][length]specifier
specifier
%s -string
%d / %i β Signed Decimal Integer
%c β Character
%f β Decimal floating point
%o β Signed octal
%u β Unsigned decimal integer
%x β Unsigned hexadecimal
%p β Pointer address
flags
- β Left Justify
+ β Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers
β (space) If no sign is going to be written, a blank space is inserted before the value
# β Used with o, x or X specifiers the value is preceded with 0, 0x or 0X respectively for values different than zero. Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written. Used with g or G the result is the same as with e or E but trailing zeros are not removed.
0 β Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see width sub-specifier).
Copy
char *strcpy(char *s1, const char *s2);β Copiess2intos1and returnss1char *strncpy(char *s1, const *s2, size_t n);β Copies at mostncharacters froms2intos1and returnss1- May not copy null character if
nis not long enough account for it.
- May not copy null character if
strlcpy( myString, "abcd", 4);
Concatenate
char *strcat(char *s1, const char *s2);β appendss2tos1overwritings1βs null terminator returns news1char *strncat(char *s1, const char *s2, size_t n);β appends n characters ofs2tos1overwritings1βs null terminator returns news1
Compare
- Compares the ASCII value of the characters to determine if itβs equal to, less than or greater
int strcmp(const char *s1, const, char *s2);β Comparess1ands2returns less then 0, equal to 0 or greater than 0, depending on ifs1is equal to, less than or greater thens2int strncmp(const char *s1, const, char *s2, size_t n);β Compares n characters ofs1ands2returns less then 0, equal to 0 or greater than 0, depending on ifs1is equal to, less than or greater thens2
Misc
char strtok(char *s1, const char *s2)β splitss1into βtokensβ which are logical pieces such as words, should be run ons1until all tokens have been created and it will returnNULLs2is the delimiting character- calls after the first call should contain
NULLas first argument to continue to tokenize initial argument.
size_t strlen(const char *s)β determines length of strings
Reference
Embedded Wednesdays: Characters. β Embedded
#coding/c #coding/cpp