===================Example 1 ===================
// i_like_x.c

// This example shows how to
// (1) Ask the user for a "string" which means some words such as "dogs and cats"
// (2) Get the user's reply
// (3) Replies, saying that the computer hates dogs/cats etc., and tell what the computer likes.

// I got the "strip_newline" procedure from here.
// http://www.cprogramming.com/tutorial/c/lesson9.html
// I wish to thank Alex Allain for writing some very nice lessons on how to use C


// Compile command for this code c version:
// gcc -Wall -W -Werror i_like_x.c -o i_like_x
// Compile command for this code c++ version:
// g++ -Wall -W -Werror i_like_x.c -o i_like_x
// To run the program type:
// ./i_like_x

#include <stdio.h> // This has the keyboard and file IO functions
#include <string.h> // This has the string functions

/* this function is designed to remove the newline from the end of a string
entered using fgets. Note that since we make this into its own function, we
could easily choose a better technique for removing the newline. Aren't
functions great? */
void strip_newline( char *str, int size ) {
int i;
/* remove the null terminator */
for ( i = 0; i < size; ++i ) {
if ( str[i] == '\n' ) {
str[i] = '\0';
/* we're done, so just exit the function by returning */
return;
}
} /* if we get all the way to here, there must not have been a newline! */
}

int main()
{
char what_you_like[100];
char what_i_like[100];
strcpy(what_i_like,"scorpions");
printf("Which animal (dogs, cats...) do you like? ");
fgets (what_you_like,100,stdin );
strip_newline(what_you_like,100);
printf ("Ha! I hate %s\n",what_you_like);
printf ("I really like %s. They are cute and cuddly.\n",what_i_like);
return(0);
}


===================Example 2 ===================
// i_like_x2.c

// This example is just like i_like_x.c, except that the computer now likes a variety of animals,
// so it can vary its answer. It does this with an array of strings.

// This example shows how to
// (1) Ask the user for a "string" which mans some words such as "dogs and cats"
// (2) Get the th user's reply
// (3) Replies, saying that the computer hates dogs/cats etc., and tell what the computer likes.

// I got the "strip_newline" procedure from here.
// http://www.cprogramming.com/tutorial/c/lesson9.html
// I wish to thank Alex Allain for writing some very nice lessons on how to use C

// Compile command for this code c version:
// gcc -Wall -W -Werror i_like_x2.c -o i_like_x2
// Compile command for this code c++ version:
// g++ -Wall -W -Werror i_like_x2.c -o i_like_x2
// To run the program type:
// ./i_like_x2

#include <stdio.h> // This has the keyboard and file IO functions
#include <string.h> // This has the string functions
#include <time.h> // I need this because I will use the time to "seed" the random function
#include <stdlib.h> // I need this because it has the random functions

/* this function is designed to remove the newline from the end of a string
entered using fgets. Note that since we make this into its own function, we
could easily choose a better technique for removing the newline. Aren't
functions great? */
void strip_newline( char *str, int size ) {
int i;
/* remove the null terminator */
for ( i = 0; i < size; ++i ) {
if ( str[i] == '\n' ) {
str[i] = '\0';
/* we're done, so just exit the function by returning */
return;
}
} /* if we get all the way to here, there must not have been a newline! */
}

int main()
{
int random_choice;
char what_you_like[100];
char what_i_like[100];
srand(time(NULL)); random_choice = rand();
switch (random_choice % 3) {
case 0: strcpy(what_i_like,"scorpions"); break;
case 1: strcpy(what_i_like,"slugs"); break;
case 2: strcpy(what_i_like,"salamanders");; break;
}
printf("Which animal (dogs, cats...) do you like? ");
fgets (what_you_like,100,stdin );
strip_newline(what_you_like,100);
printf ("Ha! I hate %s\n",what_you_like);
printf ("I really like %s. They are cute and cuddly.\n",what_i_like);
return(0);
}

===================Example 4 ===================
// ===================== get_individual_characters.c ===============

// Compile command for this code, c version:
// gcc -Wall -W -Werror get_individual_characters.c -o get_individual_characters
// Compile command for this code, c++ version:
// g++ -Wall -W -Werror get_individual_characters.c -o get_individual_characters

// C is not the greatest language for word processing but even so we need to learn
// how to use it. I got this example from here
// http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044652485&id=1043284385

// The same author has a nice little explanation of the weird issues of C strings here
// http://www.cprogramming.com/tutorial/c/lesson9.html

// The printf command is well documented here:
// http://www.cplusplus.com/reference/cstdio/printf/

// Compile command for this code c version:
// gcc -Wall -W -Werror get_individual_characters.c -o get_individual_characters
// Compile command for this code c++ version:
// g++ -Wall -W -Werror get_individual_characters.c -o get_individual_characters
// To run the program type:
// ./get_individual_characters

#include <stdio.h> // This has the keyboard and file IO functions
#include <string.h> // This has the string functions

int main()
{
//char buf[BUFSIZ];
char buf[10];
char *p;
int n;
printf ("Please enter a line of text, max %d characters\n", sizeof(buf));
if (fgets(buf, sizeof(buf), stdin) != NULL) {
printf ("Thank you, you entered >%s<\n", buf);
// Test for, and remove the newline character that is at the end of the string
if ((p = strchr(buf, '\n')) != NULL) *p = '\0';
printf ("And now it's >%s<\n", buf);
}
printf("Here are the individual characters:\n");
for (n=0;n<(int)strlen(buf);n++) {
printf("%d %c\n",n,buf[n]);
}
return 0;
}

===================Example 5 ===================