Old Course Notes Strings
Old Course Notes Strings
char str[10];
141
Chapter 9
Strings may be input using the scanf() function using the “%s” format
specification. An example would be
scanf(“%s”, str);
scanf() works by looking for the first non-space character entered, and
continues to read the input characters until it reads another space character.
When a space is reached, then a ‘\0’ is written. For example, the two strings
“ abc “ and “abc” used as inputs for the above scanf() statement are
equivalent.
The gets() function reads the input characters until a newline character
‘\n’ is met. The newline character is replaced by a null character inside the
string.
Format:
gets(<string variable>);
142
Strings
Example.
gets(str);
Given the sample gets statement, the two input strings “ abc “ and
“abc” are not anymore equivalent.
Strings can be output using the printf() function with "%s” as the format
specification.
Example.
printf(“%s\n”, str);
printf(“%s”, “Hello!\n”);
The function puts() can also be used to output a string. The syntax is as
follows:
Format:
puts(<string>);
Example.
puts(str);
puts(“What is your name?”);
To get the length of the string, we use the strlen function of the string library.
Format:
143
Chapter 9
Example.
Since strings are array of characters, they cannot be assigned using the
assignment statement (unless it is part of the declaration). To copy a string to
another, the strcpy function is used. The strcpy has the following format:
Format:
Example.
Unlike integers, float, and characters, we cannot use the relational and equality
operators to compare strings. Each of the characters in the string should be
compared. The string library of C provides this capability of comparing each
character through the strcmp function.
Format:
144
Strings
Example.
char str1[10] = “hello”;
char str2[10] = “hellO”;
strcmp(str1, str2) will return a positive value (in this case, 32) because
the ASCII value of ‘O’ (79) is less than the ASCII value of ‘o’ (111).
str1
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
str2
char strWord[5];
strWord = “Hi”;
3. It is wrong to use the a relational or equality operator within the call to strcmp. The
strcmp function accepts 2 string parameters.
145
Chapter 9
4. In using puts and gets, there should be no conversion characters. The puts and gets
functions are strictly for strings. Thus, the following statements are wrong:
char string5[6];
gets(“%s”, string5);
puts(“%s”, string5);
gets(“%d”, string5);
puts(“%d”, string5);
5. There is no need to put the & sign in a gets or a scanf statement since a string is an
array of characters. Remember that when an array is passed as a parameter, it is the
address that is passed. Thus, it is wrong to have statements such as the following:
char string5[6];
gets(&string5);
scanf(“%s”, &string5);
Chapter Exercises
#include <stdio.h>
#include <string.h>
main()
{
str9 strName1;
str9 strName2 = “jane do”;
str50 strMyName, strOtherPerson;
int nSize, nCompare;
nSize = strlen(strName2);
printf(“Size of strName2 is %d\n”, nSize);
146
Strings
printf(“\n%s\n” , MESSAGE);
printf(“Scientist %s shows %s ”, strOtherPerson,
strMyName);
puts(“as proof that alien life form exist.”);
}
2. Create a function that will continuously ask for a string input from the user until
the input string has at least 6 characters. Assume that the maximum string to be
given by the user is 10 characters.
3. Given an array of 100 names, create a function findSubstring that will copy all
names containing a substring equal to strKey. Use the following function
outline:
147