Академический Документы
Профессиональный Документы
Культура Документы
In this tutorial you will learn about Initializing Strings, Reading Strings from the terminal, Writing strings to
screen, Arithmetic operations on characters, String operations (string.h), Strlen() function, strcat() function,
strcmp function, strcmpi() function, strcpy() function, strlwr () function, strrev() function and strupr() function.
In C language, strings are stored in an array of char type along with the null terminating character "\0" at the
end. In other words to create a string in C you create an array of chars and set each element in the array to a
char value that makes up the string. When sizing the string array you need to add plus one to the actual size of
the string to make space for the null terminating character, \0
1. char fname[4];
The above statement declares a string called fname that can take up to 3 characters. It can be indexed just as a
regular array as well.
fname[] = {t,w,o};
Character t w o \0
The last character is the null character having ASCII value zero.
Initializing Strings
To initialize our fname string from above to store the name Brian,
You can observe from the above statement that initializing a string is same as with any array. However we also
need to surround the string with quotes.
If you wanted to print a string from a variable, such as our fname string above you can do this:
You can insert more than one variable, hence the in the prototype for printf but this is sufficient. Use %s
to insert a string and then list the variables that go to each %s in your string you are printing. It goes in order of
first to last. Lets use a first and last name printing example to show this:
The first name would be displayed first and the last name would be after the space between the %ss.
Say we want to allow the user to enter a name from stdin. Aside from taking the name as a command line
argument, we can use the scanf function which has the following prototype:
getname.c:
1. #include <stdio.h>
2.
3. void main() {
4.
5. char fname[30];
6. char lname[30];
7.
8. printf("Type first name:\n");
9. scanf("%s", fname);
10.
11. printf("Type last name:\n");
12. scanf("%s", lname);
13.
14. printf("Your name is: %s %s\n", fname, lname);
15. }
We declare two strings fname and lname. Then we use the printf function to prompt the user for a first name.
The scanf function takes the input from stdin and automatically exits once the user presses enter. Then we
repeat the above sequence except using the last name this time. Finally we print the full name that was typed
back to stdout. Should look something like this:
charmath.c:
1. #include <stdio.h>
2. void main() {
3. unsigned char val1 = 20;
4. unsigned char val2 = 30;
5. int answer;
6.
7. printf("%d\n", val1);
8. printf("%d\n", val2);
9.
10. answer = val1 + val2;
11. printf("%d + %d = %d\n", val1, val2, answer);
12.
13. val1 = 'a';
14. answer = val1 + val2;
15.
16. printf("%d + %d = %d\n", val1, val2, answer);
17. }
First we make two unsigned character variables and give them (rather low) number values. We then add them
together and put the answer into an integer variable. We can do this without a cast because characters are an
alphanumeric data type. Next we set var1 to an expected character value, the letter lowercase a. Now this next
addition adds 97 to 30, why?
Because the ASCII value of lowercase a is 97. So it adds 97 to 30, the current value in var2. Notice it did not
require casting the characters to integers or having the compiler complain. This is because the compiler knows
when to automatically change between characters and integers or other numeric types.
String Operations
Character arrays are a special type of array that uses a \0 character at the end. As such it has it is own header
library called string.h that contains built-in functions for performing operations on these specific array types.
You must include the string header file in your programs to utilize this functionality.
1. #include <string.h>
We will cover the essential functions from this library over the next few sections.
Length of a String
Use the strlen function to get the length of a string minus the null terminating character.
1. int strlen(string);
If we had a string, and called the strlen function on it we could get its length.
Concatenation of Strings
The strcat function appends one string to another.
The first string gets the second string appended to it. So for example to print a full name from a first and last
name string we could do the following:
1. char fname[30] = {"Bob"};
2. char lname[30] = {"by"};
3. printf("%s", strcat(fname, lname));
The return value indicates how the 2 strings relate to each other. if they are equal strcmp returns 0. The value
will be negative if string1 is less than string2, or positive in the opposite case.
For example if we add the following line to the end of our getname.c program:
When run on a Linux computer with the following first and last name combinations, the program will yield the
following output.
Imagine using this function in place of strcmp in the above example, all of the first and last combinations
would output 0.
Copy Strings
To copy one string to another string variable, you use the strcpy function. This makes up for not being able to
use the = operator to set the value of a string variable.
1. strcpy(string1, string2);
To set the first name of our running example in code rather than terminal input we would use the following:
1. strcpy(fname, "Bob");
1. strlwr(string);
This will convert uppercase characters in string to lowercase. So BOBBY would become bobby.
1. strrev(string);
Will reverse the order of string. So if string was bobby, it would become ybbob.
strupr(string);
This will convert lowercase characters in string to uppercase. So bobby would become BOBBY.