Table of Contents
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.h
-
string.h
– String Library stddef.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);
– Copiess2
intos1
and returnss1
char *strncpy(char *s1, const *s2, size_t n);
– Copies at mostn
characters froms2
intos1
and returnss1
- May not copy null character if
n
is not long enough account for it.
- May not copy null character if
strlcpy( myString, "abcd", 4);
Concatenate
char *strcat(char *s1, const char *s2);
– appendss2
tos1
overwritings1
’s null terminator returns news1
char *strncat(char *s1, const char *s2, size_t n);
– appends n characters ofs2
tos1
overwritings1
’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);
– Comparess1
ands2
returns less then 0, equal to 0 or greater than 0, depending on ifs1
is equal to, less than or greater thens2
int strncmp(const char *s1, const, char *s2, size_t n);
– Compares n characters ofs1
ands2
returns less then 0, equal to 0 or greater than 0, depending on ifs1
is equal to, less than or greater thens2
Misc
char strtok(char *s1, const char *s2)
– splitss1
into “tokens” which are logical pieces such as words, should be run ons1
until all tokens have been created and it will returnNULL
-
s2
is the delimiting character - calls after the first call should contain
NULL
as 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