String 21

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

String

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

printf("Topic: %s\n", string_var);


printf ("***%8s***%3s***\n", "Short", "Strings");
printf("%-20s\n", president);
Character-Handling Library
<ctype.h>
Some String Library Functions from string.h
String Library Functions: Assignment and Substrings

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.

strncpy(one_str, "Test string", 20);

strncpy(one_str, “A very long test string”, 20);

strncpy(dest, source, dest_len - 1);


Substrings dest[dest_len - 1] = '\0';
char *strncpy(char *dest, const char *source, size_t n);
char result[10], s1[15] = "Jan. 30, 1996"; strncpy(result, &s1[5], 2);
strncpy(result, s1, 9); result[2] = '\0';
result[9] = '\0'; Execution of Execution of
strncpy(result, s1, 9); strncpy(result, &s1[5], 2);
char *last, *first, *middle;
char pres[20] = "Adams, John Quincy";
char pres_copy[20];
strcpy(pres_copy, pres);

last = strtok(pres_copy, ", ");


first = strtok(NULL, ", ");
middle = strtok(NULL, ", ");
trim_blanks( n_string, a_string)

char * trim_blanks( char *trimmed, const char *to_trim)

/* 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.

char * strcat (char *dest, const char *source)

strcat Appends source to the end of dest :


strcat(s1, "and more");

char * strncat (char *dest, const char *source, size_t † n)


strncat(s1, "and more", 5);

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);

if (strlen(s1) + strlen(s2) < STRSIZ) {


strcat(s1, s2);
} else {
strncat(s1, s2, STRSIZ - strlen(s1) - 1);
s1[STRSIZ - 1] = '\0'; 24 16 24
} Jupiter SymphonySymphony
printf("%s\n", s1); Jupiter SymphonySym
You can see from our examples of the use of strcpy , strncpy , strcat ,
and strncat that there are two critical questions that must always be in the mind

of a C programmer who is manipulating strings:

■ Is there enough space in the output parameter for the entire string being created?

■ Does the created string end in '\0' ?


strcpy , strncpy , strcat , strncat

■Is there enough space in the output parameter for the entire
string being created?

■ Does the created string end in '\0' ?


String Comparison

Possible Results of strcmp(str1, str2)


Arrays of Pointers

Exchanging String Elements of an Array

strcpy (list [index_of_min], list[fill]);

strcpy(temp, list[index_of_min]);
strcpy(list[index_of_min], list[fill]);
strcpy(list[fill], temp);
Arrays of Pointers

Exchanging String Elements of an Array

strcpy (list [index_of_min], list[fill]);

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ı

Kullanıcıdan alınan 4 band rengine göre direnç


değerini hesaplayıp ekrana yazan program
yazın.

Program Çıktısı :

1. Band : Mor
2. Band : Yeşil
3. Band : Kahverengi
4. Band : Altın

Direnç Değeri : 750 ohm

Tekrar direnç değeri hesaplatmak istiyor musunuz (E/H) ?

You might also like