Theory Exercises

The operating system on your computer system shares the processor (assuming there is only one processor) with many other programs. However, we understand that at any given moment, the processor can only execute the instructions from one program. Explain how can the operating system and several other programs run on the same processor at the same time.

C Programming Exercises

2. Create a C source code getinput.c that prompts the user to enter his full name. Save it in an appropriately named file such as getFullName.c. Compile it and execute it.

Some of you may realize that the program name a.out isn't always a good choice. If you wants to use a different name for your executable such as getFullName, use the option -o of gcc, as in gcc getFullName.c -o getFullName. The option "-o" stands for "Output".

Recompile your program to create an executable named getFullName.

Note that, by convention, Unix commands or programs do not use extension names, unlike Windows (which uses .exe and .com). Therefore please do not use extension names for your Unix executables.

In the above program, we use the standard I/O function fgets to read a line of input from a file, which in this case is the standard input (stdin). Both fgets and stdin are defined in the standard I/O library, therefore, you must include header file < stdio.h > in your program.

stdin (standard input) is similar to System.in in Java, and cin in C++. stdout (standard output) is similar to System.out in Java, and cout in C++. stderr (standard error) is similar to System.err in Java, and cerr in C++.

Note also in the above program, we read a line into a character array of size 128 characters. If you end the keyboard input with a ENTER key, fgets will store the newline character in the character array. In some applications, you may need to strip this character off from the char array. For example the following code can be used to strip the newline character away from the char array line:

n = strlen(line);
if (line[n-1] == '\n')
line[n-1] = '\0';

In C, the last character in a string must always be the null character "\0".

3. Write a C program that reads a line of input from the standard input and then reverse the line. Finally print out the reversed line on the standard output.

You may want to use the standard string function strlen to find out the number of characters in a character array. By typing the command man -s 3 strlen, you will see that you must include string.h header file.

You may also want to use C's for-loop for this question. The following example shows how to loop through an array:

n = strlen(line);
for ( i = 0; i < n; ++i ) {
c = line[i];
}

where n is an integer variable containing the number of characters in the char array line, i is an integer variable which acts as the loop index, c is a char variable, and line is a character array.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.