Strings in C

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

Printing

C library function printf()

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); – Copies s2 into s1 and returns s1
  • char *strncpy(char *s1, const *s2, size_t n); – Copies at most n characters from s2 into s1 and returns s1
    • May not copy null character if n is not long enough account for it.
  • strlcpy( myString, "abcd", 4);

Concatenate

  • char *strcat(char *s1, const char *s2); – appends s2 to s1 overwriting s1’s null terminator returns new s1
  • char *strncat(char *s1, const char *s2, size_t n); – appends n characters of s2 to s1 overwriting s1’s null terminator returns new s1

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); – Compares s1 and s2 returns less then 0, equal to 0 or greater than 0, depending on if s1 is equal to, less than or greater then s2
  • int strncmp(const char *s1, const, char *s2, size_t n); – Compares n characters of s1 and s2 returns less then 0, equal to 0 or greater than 0, depending on if s1 is equal to, less than or greater then s2

Misc

  • char strtok(char *s1, const char *s2) – splits s1 into “tokens” which are logical pieces such as words, should be run on s1 until all tokens have been created and it will return NULL
    • 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 string s

Reference

Embedded Wednesdays: Characters. — Embedded

#coding/c #coding/cpp