Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
This page looks at the string as a character array and endfile loop processing.
Note: For example purposes, declarations, input, processing, and output will not be separated in the following program.
In all cases, the output is exactly the same.
2. String library
C has a string library.
3. String literals
Here is one way to represent the string "A bad cat.".
The strlen function from the string.h library returns the length of a string.
4. Array of characters
A string in C is an array of characters. A character array literal can be used as a string, but the terminating null byte (zero) must be included at the end.
5. String input
A string can be input from the standard input.
6. Explicit processing
The string c1 can be processed (after input using scanf) explicitly, character by character, as follows.
7. Program
Here is the C code.
8. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
9. Program
Here is a program that explicitly reads in a string, character by character, until the end of file is detected.
Here is the C code.
10. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
11. String length function
We can write our own string length function as follows.
The function strlen in string.h is a similar function.
Do you see how the rsStrLen function could not work right if the zero byte is not at the end? This and related issues make string processing tedious and hard to get always correct in languages such as C.
12. Constant arrays
In C, the keyword const is a hint to the compiler that this variable is not allowed to be changed. We can use that in the above function rsStrLen1 to get function rsStrLen2 as follows.
13. Pointers
As if the above is not confusing enough, in C one can use a pointer as a formal parameter instead of empty array brackets as follows.
Here is the const version using a pointer formal parameter.
Remember, the const keyword means that the variable is a pseudo-constant and cannot be changed.
Note: In all of the above four functions, the body is exactly the same. What has changed is the formal parameter signature.
Rule: Arrays are always passed by reference. But a variable passed by reference as a pointer can be accessed as an array. This can cause bizarre behavior if the passed variable is not actually an array. See Arrays and lack of bounds checking .
14. Program
Here then is a program that explicitly reads in a string, character by character, until the end of file is detected.
Here is the C code.
15. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.