1 / 8

Character String Manipulation

Character String Manipulation. Overview. Character string functions sscanf() function snprintf() function. Some Character String Functions. double atof (const char *string); - Converts string into a floating point value int atoi (const char *string);

min
Download Presentation

Character String Manipulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Character String Manipulation

  2. Overview • Character string functions • sscanf() function • snprintf() function

  3. Some Character String Functions double atof(const char *string); - Converts string into a floating point value intatoi(const char *string); - Converts string into an int value char *strncat(char *s1, const char *s2, size_t count); - Appends up to count characters of s2 onto the end of s1char *strchr(const char *s, int c); - Searches for first occurrence of c in s. Returns a pointer to c or NULLintstrncmp(const char *s1, const char *s2, size_t count); - Compares up to count characters of s1 to s2. Returns 0 if they are identicalchar *strncpy(char *s1, const char *s2, size_t count); - Copies up to count characters of s2 onto s1size_tstrlen(const char *s); - Returns the number of characters in s not counting ‘\0’char *strtok(char *s1, const char *s2); - Extracts tokens from string s1 based on token separators in s2

  4. sscanf() Function • #include <stdio.h>int sscanf(const char *buffer, const char *format, …); • The sscanf() function is identical to scanf() except that data is read from the array pointed to by buffer rather than stdin • The return value is equal to the number of variables that were actually assigned values • A value of zero means that no fields were assigned any values • As a side note, there is also an snscanf() function, but it is not standardized in C

  5. snprintf() Function • #include <stdio.h>int snprintf(char *buffer, int size, const char *format, …); • The snprintf() function is identical to printf() except that the output is put into the array pointed to by buffer instead of being written to stdout • The array pointed to by buffer should already be allocated • The return value is equal to the number of characters actually placed into the array • The size parameter provides a bounds check on the array pointed to by buffer. It is the maximum number of characters that will be written to the buffer

  6. Example use of strncpy(), sscanf() and snprintf() #include <stdio.h> #define MAX_LENGTH 100 int main(void) { char stringA[MAX_LENGTH]; char stringB[MAX_LENGTH]; int count; float costPerItem; int binNbr; char name[MAX_LENGTH]; float totalCost; (More on next slide)

  7. Example use strcpy(), sscanf() and sprintf() strncpy(stringA, "103 67.4 35bottle“, MAX_LENGTH); sscanf(stringA, "%d %f %d%s", &count, &costPerItem, &binNbr, name); fprintf(stderr, "Input data: %d %.2f %d %s\n\n", count, costPerItem, binNbr, name); totalCost = count * costPerItem; snprintf(stringB, MAX_LENGTH, "%d %s items * $ %.2f per %s = $ %.2f", count, name, costPerItem, name, totalCost); printf("Computation for Bin # %d:\n”, binNbr); printf(“ %s\n", stringB); return 0; } // End main

  8. Sample output Input data: 103 67.40 35 bottle Computation for Bin # 35: 103 bottle items * $ 67.40 per bottle = $ 6942.20 

More Related