Arrays of
Char VS
C Strings
Action Array of char String
Declare variable: char array [5]; char cstr [5]; // stores
only 4 chars of a string
Declare variable char
array [5] = { 'A', 'B', 'C', 'D', 'E' };
char cstr [5] = "ABCD";
and initialize: or
char cstr[5]
= { 'A', 'B', 'C', 'D', '\0' };
or char array
[] = { 'A', 'B', 'C', 'D', 'E' };
or char cstr [] = "ABCD";
or char cstr[5] = { 'A', 'B', 'C', 'D', '\0' };
Print first char: cout << array[0]; cout
<< cstr[0];
Print all chars: for
(n = 0; n < 5; n++) cout << cstr;
cout << array[n];
or for (n = 0; cstr[n]
!= '\0'; n++)
cout
<< cstr[0];
Read first char: cin >> array[0];
cin >> cstr[0];
or
array[0] = cin.get(); or
cstr[0] = cin.get();
or cin.get(array[0]); or cin.get(cstr[0]);
Read all chars: for
(n = 0; n < 5; n++) cin
>> cstr;
// reads up to whitespace
cin >> array[n]; or
for (n = 0; n < 4; n++)
cin
>> cstr[n];
cstr[n] =
'\0';
Assign first char char
ch; char ch;
to another variable: ch = array[0]; ch = cstr[0];
Assign all chars char
copy [5]; char copy
[5];
to another
for (n = 0; n < 5; n++) strcpy (copy,
cstr);
variable: copy [n] = array
[n]; or strcpy (copy, "CSCI");
or // same as for
the array of char
Comparison: strcmp (cstr, copy)
returns a value less than 0 if cstr
< copy
or a value of 0 if cstr == copy
or a value greater
than 0 if cstr > copy
Test for equality: equal
= true; if
( strcmp (cstr, copy) == 0
) ...
for (n = 0; n < 5; n++)
if (array[n] != copy[n])
{
equal = false;
break;
}
String Operations found
in C++ library <cstring>
Operation Description
--------------------------------------------------------------------------------------------------------------------------
strcat (s1, s2) Appends
s2 to s1
strncat (s1, s2, n) Appends
at most n characters of s2 to s1
strcy (s1, s2) Copies
s2 into s1
strncpy (s1, s2, n) Copies
first n charcters of s2 into s1
strlen (s) Returns
length of s (not counting the terminating null character)
strcmp (s1, s2) Compares
s1 with s2 and returns an integer less than, equal to,
or
greater than 0 according to whether s1 is less than, equal to,
or
greater than s2
strncmp (s1, s2, n) Same as strcmp, but compares only the first n characters of s1 and
s2
strchr (s, c) Returns
a pointer to the first occurrence of c (character) in s, or NULL if not found
strrchr (s, c) Like strchr but locates the last occurrence of c in s
strstr (s1, s2) Returns
a pointer to the first occurrence in s1 of s2, or NULL if not found
strpbrk (s1, s2) Returns
a pointer to the first occurrence in s1 of any character of s2, or NULL if not
found
strspn (s1, s2) Returns
the number of characters in s1 before any character in s2
strcspn (s1, s2) Returns
the number of characters in s1 before a character not in s2
String Operations found
in C++ library <cstdlib>
--------------------------------------------------------------------------------------------------------------------------------
double atof (s) Returns the value obtained by
converting the character string s to double
int atoi (s) Returns
the value obtained by converting the character string s to int
long atoll (s) Returns
the value obtained by converting the character string s to long int