C Strings

Optional.
An array of char is like a string:
	char name[max_name];
	char code[10];
A string constant (a string literal) is a string of chars in quotation marks:
	"hello"
	"Joe Smith"
	"a"		//not a char constant
	""		//null string, empty string, 0 chars
Can input or output a string with cin and cout:
char name[20];

cout << "Enter a name: ";
cin >> name;
cout << "Welcome " << name;
The input will read the first "word" only, so if user enters: Joe Smith only "Joe" will be input. The "Smith" will still be in the input stream. cin reads the chars up to the first space, tab, or newline.

cin adds a null character '\0' to the end of the inputted chars in the array.

cout outputs the characters of the array up to the null character

Elements 4 thru 19 have garbage values. (leftovers)

Since C strings are so useful (names of people, places, things; text) there are many built-in functions to process them.

#include <cstring>
strlen function to find the length of a C string; the number of chars in the C string not including the null character. The C string argument must have a null character to mark its end.
Prototype:
int strlen (char []);    //returns length/size/# of chars of C string argument

cout << "Your name has " << strlen(name) << " chars";
If name array had C string "Joe", strlen function returns 3.

These C string functions do not know the physical size of the char arrays that are their arguments. The null character indicates the end of the virtual array. If no null character, bad things happen.

strcmp function to compare two C strings for lexicographic ordering (dictionary order).

Can not use ==, !=, <, etc. with C strings.

Prototype:
int strcmp (char [], char []);
 Returns <0 (usually -1 but compiler-dependant) 
if first C string is less than second C string.
 Returns 0 if the C strings are the same.
 Returns >0 if the first C string is greater than second C string.

//input 2 C strings, find their relative dictionary ordering
char word1[max_word], word2[max_word];
cout << "Enter two words: ";
cin >> word1 >> word2;
if (strcmp(word1,word2) < 0)
  cout << word1 << " is before " << word2;
else if (strcmp(word1,word2) == 0)
  cout << word1 << " is same as " << word2;
else
  cout << word1 << " is after " << word2;

Run:
user enters:  	cat  	dog
Output:	cat is before dog

user enters: 	cattle	cat
Output:	cattle is after cat

user enters:   ca$\?ar	ca$\#ar
Output:  	ca$\?ar is after ca$\#ar           
because ? is ASCII 63, # is ASCII 35.  "Asciibetical"
Here's the program: asciibet.cpp

A C string constant can be an argument to a function that takes a char array argument.

A C string constant has a null character at the end of it:
"hello" compiler adds the null character to a C string constant

char word[30];
cout << "Enter the secret word: ";
cin >> word;
if (strcmp(word, "aardvark") == 0)
  cout << "Correct!";
else
  cout << "Sorry, you aren't cool";


cout << "Length of aardvark is " << strlen("aardvark");
//useless, since we can count the chars of a C string constant

Can not use = with C strings.
strcpy function copies a C string (constant or char array) to a C string variable (char array).
strcpy(destination char array, source C string)
strcpy(to,from)

char s[max_string];
strcpy(s,"Natasha");   //string's value is Natasha
strcpy(s,"Boris");     //now it's Boris   \0 copied too

contents of s:  Boris\0a\0...



char word1[30], word2[30], temp[30];
...
//swap the two words
strcpy(temp,word1);
strcpy(word1,word2);
strcpy(word2,temp);
---------------------------------
char array can be initialized at declaration:
 
char name[max_name]="Smith"; //only place = can be used with C strings


These built-in functions are simple:
int our_strlen (char s[]) {
  int size=0;
  while (s[size] != '\0')   // until end of string
    size++;
  return size;
}


void our_strcpy (char dest[], char src[]) {
  int i=0;
  while (src[i] != '\0') {
    dest[i] = src[i];
    i++;
  }
  dest[i] = '\0';    //copy a null char to mark end of dest
}
--------------------------------------------------

Review of data types:

Type		Variable			Constant
char		char var	char c;		'a'		
string		string var	string s;	"hello"
C string	char array	char s[9];	"hello"
int		int var		int i;		5
float		float var	float f;	5.0		




2D array of chars. Each row is a 1D array of chars.

Each row will be a name:
char names[max_people][max_name];
int people;

cout << "Enter number of people: ";
cin >> people;
for (i=0; i< people; i++) {
  cout << "Enter a name: ";
  cin >> names[i];
}


strcpy(names[i],names[j]);    //copy element j to element i

strcpy(names[3],"Smith");	//Smith in row 3
names[3][2] = 'y';         		//now its Smyth


//Output the length of each name and the name
for (i=0; i< people; i++)
  cout << strlen(names[i]) << setw(max_name) 
       << names[i] << endl;


Program to do Morse code morse.cpp

Program to input names into 2D array in sorted order sortnames.cpp

Next (structs)


©David Wills