String 21
String 21
String 21
References
C How to Program, Paul Deitel, Harvey Deitel
Problem Solving and Program Design in C, Jeri R. Hanly, Elliot B. Koffman
Declaring and Initializing String Variables
• char string_var[30];
• char str[20] = "Initial value";
char month[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August","September",
"October", "November", "December"};
Input/Output with printf and scanf
char one_str[20];
one_str = "Test string"; /* Does not work */
String Assignment
would overflow one_str , storing the final characters 'i' , 'n' , 'g' , and '\0' in
strcpy(one_str, "Test String"); memory allocated for other variables. The values of these other variables would
strcpy(one_str, "A very long test string"); seem to change spontaneously. On rare occasions, such overflow would
generate a run-time error message.
/* output */ /* input */
Longer Strings: Concatenation and Whole-Line Input
String library functions strcat and strncat modify their first string argument by adding all or part of their
Concatenation second string argument at the end of the first argument. Both functions assume that sufficient
space is allocated for the first argument to allow addition of the extra characters.
strncat Appends up to n characters of source to the end of dest , adding the null character if necessary:
if (strlen(s1) + strlen(s2) < STRSIZ) {
strcat(s1, s2);
} else {
strncat(s1, s2, STRSIZ - strlen(s1) - 1);
s1[STRSIZ - 1] = '\0';
}
#define STRSIZ 20
char s1[STRSIZ] = "Jupiter ",
s2[STRSIZ] = "SymphonySymphony";
printf("%d %d %d\n", strlen(s1), strlen(s2), strlen(strcat(s1, s2)));
printf("%s\n", s1);
■ Is there enough space in the output parameter for the entire string being created?
■Is there enough space in the output parameter for the entire
string being created?
strcpy(temp, list[index_of_min]);
strcpy(list[index_of_min], list[fill]);
strcpy(list[fill], temp);
Arrays of Pointers
strcpy(temp, list[index_of_min]);
strcpy(list[index_of_min], list[fill]);
strcpy(list[fill], temp);
String Review
1. Strings in C are arrays of characters ended by the null character '\0' .
2. String input is done using scanf and fscanf with %s descriptors for strings separated by whitespace,
using gets and fgets for input of whole lines, and using getchar and getc for single character input.
3. String output is done using printf and fprintf with %s descriptors; putchar and putc do single-
character output.
4. The string library provides functions for string assignment and extraction of substrings ( strcpy and
strncpy ), for string length ( strlen ), for concatenation ( strcat and strncat ), and for alphabetic
comparison ( strcmp and strncmp ).
5. The standard I/O library includes functions for string-to-number ( sscanf ) and number-to-string (
sprintf ) conversion.
6. String-building functions typically require the calling module to provide space for the new string as an
output parameter, and they return the address of this parameter as the function result.
7. String manipulation in C requires the programmer to take great care to avoid overflow of string
variables and creation of strings not ending in '\0' .
8. Multiple orderings of a list of strings can be maintained by storing the strings once and creating
arrays of pointers for additional orderings.
9. The ctype library provides functions for classification and conversion of single characters.
Direnç Değerini Okuma
Ödev
Direnç Değeri Okuma Programı
Program Çıktısı :
1. Band : Mor
2. Band : Yeşil
3. Band : Kahverengi
4. Band : Altın