1 #include <stdio.h> 2 #include <stdlib.h>
3
#include <ctype.h>
4
#include <string.h>
5
6
int main(int argc, char *argv[])
7
{
8
/*Declare and initialise our variables*/
9
char FirstName[50] = "";
10
char SecondName[50] = "";
11
char FullName[110] = "";
12
int LoopIndex = 0;
13
int UpperCase = 0;
14
int LowerCase = 0;
15
int Punctuation = 0;
16
17
printf("Please enter your first name\n");
18
scanf("%s",FirstName);
19
printf("Please enter your family name\n");
20
scanf("%s",SecondName);
21
22
/*Copy FirstName into FullName*/
23
strcpy(FullName,FirstName);
24
/*Use strcat to add a space after first name*/
25
strcat(FullName," ");
26
/*Use strcat to append the FamilyName*/
27
strcat(FullName,SecondName);
28
29
printf("\nYour full name is %s\n",FullName);
30
printf("Your first name is %d characters long\n",strlen(FirstName));
31
32
/*Loop through each letter in the string and keep a count of
33
upper and lower case letters plus punctuation marks*/
34
for(LoopIndex = 0; LoopIndex < strlen(FullName); LoopIndex++)
35
{
36
if(islower(FullName[LoopIndex]))
37
LowerCase++;
38
else if (isupper(FullName[LoopIndex]))
39
UpperCase++;
40
if(ispunct(FullName[LoopIndex]))
41
Punctuation++;
42
}
43
44
printf("\nYour full name contains:\n");
45
printf("%-3d Lower case letters\n",LowerCase);
46
printf("%-3d Upper case letters\n",UpperCase);
47
printf("%-3d Punctuation mark",Punctuation);
48
49
/*use the shorthand if ?: to add an s to the end of the previous
50
output if punctuation equals anything other than 1*/
51
printf("%c\n\n",Punctuation == 1?' ':'s');
52
53
system("PAUSE");
54
return 0;
55
}
Purpose of the program
This
program
Break down of the code
Line 1 - This line starts with
a '#' hash sign which indicates that it is a preprocessor directive. The
#include directive instructs the preprocessor to copy the contents of the
following file into this source code file at this point. The filename is
surrounded with '<>' brackets this instructs the preprocessor to look in the
system 'include' directory first for this file. So this line copies the contents
of the file 'stdio.h' into the source code file at this point before compiling
starts. The file 'stdio.h' contains the declaration of many basic input output functions
that are used in C programs.
Line 2 - This
line works in the same manner as line 1. The difference is that the preprocessor
copies the contents of the file 'stdlib.h' in to the source code at this point
before compiling starts. The file 'stdlib.h' contains the declaration of many
basic functions used by C programs.
Line 3 - Is
blank.
Line 6 - This
line contains the start of our definition of the main function. As you can see
main returns an int value. This value indicates to the operating system whether
or not the program has terminated normally. The main function also takes two
parameters these contain any command line options that are sent to you program
when it starts. E.g. if you start notepad from the command line you would write
notepad if you want to start notepad with a file to edit you would write notepad
readme.txt. readme.txt would be a command line option. This is not a subject
that will be considered further in the book so you may wish to find out more
yourself.
Line 7 - This
line contains the opening brace of the main function. There must be a closing
brace to match every opening brace and vice versa. The closing brace is on line
13.
Line 53 - The
function system("PAUSE") is used to pause the program until a key is pressed.
The reason for this is that under windows the console would open, the text would
be output to it then it would close. You would see little more than a flash on
the screen as this happened. Try commenting out the line to see this happen.
Line 54 - The
return statement is used at the end of functions to return an item of data. This
item of data should match the return type declared at the start of the function.
We are in the main function and as you can see in line 6 this should be an
integer. The return statement marks the end of the function any code after this
will not be executed your compiler will usually warn you of this when you
compile the program. The value 0 is a value that tells the
operating system that the program has terminated normally.
Line 55 - This
line contains the closing brace of the main function.