8
Character String Manipulation

Character String Manipulation

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Character String Manipulation

Character String Manipulation

Page 2: Character String Manipulation

Overview

• Character string functions

• sscanf() function

• sprintf() function

Page 3: Character String Manipulation

Some Character String Functionsdouble atof(const char *string);

- Converts string into a floating point valueint atoi(const char *string);

- Converts string into an int valuechar *strcat(char *s1, const char *s2);

- Appends s2 onto the end of s1char *strchr(const char *s, int c);

- Searches for first occurrence of c in s int strcmp(const char *s1, const char *s2);

- Compares s1 to s2char *strcpy(char *s1, const char *s2);

- Copies s2 onto s1size_t strlen(const char *s);

- Returns the number of characters in schar *strstr(const char *s1, const char *s2);

- Searches for s2 in s1 char *strtok(char *s1, const char *s2);

- Extracts tokens from string s1 based on token separators in s2

Page 4: Character String Manipulation

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

Page 5: Character String Manipulation

sprintf() Function

• #include <stdio.h>

int sprintf(char *buffer, const char *format, …);

• The sprintf() 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 be null terminated

• The return value is equal to the number of characters actually placed into the array

• The sprintf() function provides no bounds checking on the array pointed to by buffer

Page 6: Character String Manipulation

Example use of strcpy(), sscanf() and sprintf()

#include <stdio.h>

#define MAX_LENGTH 50

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)

Page 7: Character String Manipulation

Example use strcpy(), sscanf() and sprintf()

strcpy(stringA, "103 67.4 35bottle“);

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;

sprintf(stringB, "%d %s items * $ %.2f per %s = $ %.2f", count, name, costPerItem, name, totalCost);

printf("Computation for Bin # %d:\n %s\n", binNbr, stringB);

return 0;} // End main

Page 8: Character String Manipulation

Sample output

Input data: 103 67.40 35 bottle

Computation for Bin # 35: 103 bottle items * $ 67.40 per bottle = $ 6942.20