As a result the final product may or may not have a null character For this

As a result the final product may or may not have a

This preview shows page 23 - 27 out of 28 pages.

before reaching the end of the source string, no null character is added. As a result, the final product may or may not have a null character. For this reason, the program sets n to one less than the size of the target array and then sets the final element in the array to the null character: strncpy(qwords[i], temp, TARGSIZE - 1); qwords[i][TARGSIZE - 1] = '\0'; This ensures that you've stored a string. If the source string actually fits, the null character copied with it marks the true end of the string. If the source string doesn't fit, this final null character marks the end of the string. The sprintf() Function The sprintf() function is declared in stdio.h instead of string.h . It works like printf() , but it writes to a string instead of writing to a display. Therefore, it provides a way to combine several elements into a single string. The first argument to sprintf() is the address of the target string. The remaining arguments are the same as for printf() a conversion specification string followed by a list of items to be written.
Image of page 23
Listing 11.24 uses sprintf() to combine three items (two strings and a number) into a single string. Note that it uses sprintf() the same way you would use printf() , except that the resulting string is stored in the array formal instead of being displayed onscreen. Listing 11.24. The format.c Program /* format.c -- format a string */ #include <stdio.h> #define MAX 20 int main(void) { char first[MAX]; char last[MAX]; char formal[2 * MAX + 10]; double prize; puts("Enter your first name:"); gets(first); puts("Enter your last name:"); gets(last); puts("Enter your prize money:"); scanf("%lf", &prize); sprintf(formal, "%s, %-19s: $%6.2f\n", last, first, prize); puts(formal); return 0; }
Image of page 24
Here's a sample run:Enter your first name:TeddyEnter your last name:BehrEnter your prize money:2000Behr, Teddy : $2000.00The sprintf()command took the input and formatted it into a standard form, which it then stored in the string formalOther String FunctionsThe ANSI C library has more than 20 string-handling functions, and the following list summarizes some of the more commonly used ones:char *strcpy(char * s1, const char * s2);This function copies the string (including the null character) pointed to by s2to the location pointed to by s1. The return value is s1char *strncpy(char * s1, const char * s2, size_t n);This function copies to the location pointed to by s1no more than ncharacters from the string pointed to by s2. The return value is s1. No characters after a null character are copied and, if the source string is shorter than ncharacters, the targetstring is padded with null characters. If the source string has nor more characters,no null character is copied. The return value is s1char *strcat(char * s1, const char * s2);The string pointed to by s2is copied to the end of the string pointed to by s1. Thefirst character of the s2string is copied over the null character of the s1string. . . .
Image of page 25
The return value is s1 . char *strncat(char * s1, const char * s2, size_t n); No more than the first n characters of the s2 string are appended to the s1 string, with the first character of the s2 string being copied over the null character of the s1 string. The null character and any characters following it in the
Image of page 26
Image of page 27

You've reached the end of your free preview.

Want to read all 28 pages?

  • Winter '15
  • Sundararajan
  • ASCII, string functions

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture