Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
89 views

How String Works

sscanf is a function used to read and interpret formatted data from strings. It parses the input string according to the format string, and stores the converted results into the variables pointed by the additional arguments. The format string contains conversion specifiers that define the type of data to retrieve and store, and it can also include flags, field width, precision, and length modifiers to control the conversion.

Uploaded by

Sagar Morje
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
89 views

How String Works

sscanf is a function used to read and interpret formatted data from strings. It parses the input string according to the format string, and stores the converted results into the variables pointed by the additional arguments. The format string contains conversion specifiers that define the type of data to retrieve and store, and it can also include flags, field width, precision, and length modifiers to control the conversion.

Uploaded by

Sagar Morje
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

How String Token Works

struct string { char str[100]; }mystring[100]; void tokenization(char strContent[], char cSep[]) { char *token; char repToken[100]; memset(&repToken, 0, sizeof(repToken)); token = strtok(strContent, cSep); int nIndex=0; while (token != NULL) { memcpy(mystring[nIndex].str, token, 4); token = strtok(NULL, cSep); nIndex++; } printf("%s\n", mystring[0].str); printf("%s\n", mystring[1].str); printf("%s\n", mystring[2].str); printf("%s\n", mystring[3].str); } void main() { char string[] = "ABC|DEF|GHI"; tokenization(string, "|"); getch(); }

Vector in Action
#include <string> #include <vector> typedef std::vector<std::string> TString; void tokenization(char strContent[], char cSep[] , TString &TStringList) { char *token; token = strtok(strContent, cSep); while (token != NULL) { TStringList.push_back(token); token = strtok(NULL, cSep); } } Code: char str1[] = "Hello World"; char str2[] = " ,\t\n"; // whatever TString TList; tokenization( str1 , str2 , TList); int nCount = TList.size(); for (int i = 0; i < nCount; i++) AfxMessageBox(TList.at(i).c_str());

Toggle Plain Text


char str[] = "Hello World"; char *b; b = &(str[3]); *b = '3'; printf("%s\n", str);

Text called from different files


char ReallyBigString[] = "really big string\r\n"; main() { printf(ReallyBigString); File2Subroutine(); } File2: extern char * ReallyBigString; File2Subroutine() { printf(ReallyBigString); }

append char to char*


char * appendCharToCharArray(char * array, char a) { char * ret = (char*)malloc(sizeof(array) + 1 + 1); strcpy(ret,array); ret[strlen(ret)] = a; ret[sizeof(ret)] = '\0'; return ret; }

Simple Char Array


// null-terminated sequences of characters #include <iostream> using namespace std; int main () { char question[] = "Please, enter your first name: "; char greeting[] = "Hello, "; char yourname [80]; cout << question; cin >> yourname; cout << greeting << yourname << "!"; return 0; }

convert unsigned char to char *


snprintf((char*)test,sizeof(test),"%s",arg); strncpy((char*)test,arg,sizeof(test));

Simple Char Pointer


char * terry = "hello"; #include <iostream> using namespace std;

Char Pointer Limitation


int main() { // This works fine: char myStr[] = "text"; myStr[0] = 'n'; cout << myStr << endl; // This does not work: char* myStr2 = "text"; myStr2[0] = 'n'; // This line results in a runtime error!!! cout << myStr2 << endl; return 0; }

char array & pointers


#include <iostream> using namespace std; int main() { char aPointer[] = "add"; char* pCarrier[sizeof(aPointer)]; pCarrier[sizeof(aPointer)] = aPointer; cout << *pCarrier; return 0; }

Example 2
char aPointer[] = "add"; char* pCarrier = &aPointer; cout << *pCarrier; return 0;

Example 3
#include <iostream> using namespace std; int main() { int i; char aPointer[] = "I love earth and all its creatures."; char *pCarrier[sizeof(aPointer)]; for (i = 0; i < sizeof(aPointer); i++) pCarrier[i] = &aPointer[i]; cout << *pCarrier; return 0; } //returns the array of char in aPointer.

Example 3
char *final = new char[100]; strcpy(final,"hiiiii");

Functions
Copying:

memcpy memmove strcpy strncpy


Concatenation:

Copy block of memory (function) Move block of memory (function) Copy string (function) Copy characters from string (function)

strcat strncat
Comparison:

Concatenate strings (function) Append characters from string

(function)

memcmp strcmp strcoll strncmp strxfrm


Searching:

Compare two blocks of memory (function) Compare two strings (function) Compare two strings using locale (function) Compare characters of two strings (function) Transform string using locale (function)

memchr strchr strcspn strpbrk strrchr strspn strstr strtok

Locate character in block of memory (function) Locate first occurrence of character in string (function) Get span until character in string (function) Locate character in string (function) Locate last occurrence of character in string (function) Get span of character set in string (function) Locate substring (function) Split string into tokens (function)

Other:

memset strerror

Fill block of memory (function) Get pointer to error message string

(function)

strlen

Get string length

(function)

Macros
NULL Null pointer
(macro)

Types
size_t Unsigned integral type
(type)

memcpy
void * memcpy ( void * destination, const void * source, size_t num ); Parameters destination Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*. source Pointer to the source of data to be copied, type-casted to a pointer of type void*. num Number of bytes to copy.

#include <stdio.h> #include <string.h> int main () { char str1[]="Sample string"; char str2[40]; char str3[40]; memcpy (str2,str1,strlen(str1)+1); memcpy (str3,"copy successful",16); printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3); return 0; } Output: str1: Sample string str2: Sample string str3: copy successful

memmove
void * memmove ( void * destination, const void * source, size_t num ); Parameters destination Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*. source Pointer to the source of data to be copied, type-casted to a pointer of type const void*. num Number of bytes to copy. /* memmove example */ #include <stdio.h> #include <string.h> int main () { char str[] = "memmove can be very useful......"; memmove (str+20,str+15,11); puts (str); return 0; }

Output: memmove can be very very useful.

sscanf function <cstdio> int sscanf ( const char * str, const char * format, ...); Read formatted data from string Reads data from str and stores them according to the parameter format into the locations given by the additional arguments. Locations pointed by each additional argument are filled with their corresponding type of value specified in the format string. Parameters str C string that the function processes as its source to retrieve the data. format C string that contains one or more of the following items:
%[flags][width][.precision][length]specifier

Where specifier is the most significant one and defines the type and the interpretation of the value of the coresponding argument: specifi Output er c Character d or i Signed decimal integer e E f g G o s u x X p n % Scientific notation (mantise/exponent) using e character Scientific notation (mantise/exponent) using E character Decimal floating point Use the shorter of %e or %f Use the shorter of %E or %f Signed octal String of characters Unsigned decimal integer Unsigned hexadecimal integer Unsigned hexadecimal integer (capital letters) Pointer address Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored. A % followed by another % character will write % to the string.

Example a 392 3.9265e +2 3.9265E +2 392.65 392.65 392.65 610 sample 7235 7fa 7FA B800:00 00

The tag can also contain flags, width, .precision and modifiers sub-specifiers, which are optional and follow these specifications: flags description

Left-justify within the given field width; Right justification is the default (see width sub-specifier). Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.

(spac If no sign is going to be written, a blank space is inserted before the value. e) Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero. Used with e, E and f, it forces the written output to contain a decimal point # even if no digits would follow. By default, if no digits follow, no decimal point is written. Used with g or G the result is the same as with e or E but trailing zeros are not removed. Left-pads the number with zeroes (0) instead of spaces, where padding is 0 specified (see width sub-specifier).

description Minimum number of characters to be printed. If the value to be printed is (numb shorter than this number, the result is padded with blank spaces. The value er) is not truncated even if the result is larger. The width is not specified in the format string, but as an additional integer * value argument preceding the argument that has to be formatted.

width

. precisio n

description

For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For e, E and f specifiers: this is the number of digits to be printed after the . decimal point. number For g and G specifiers: This is the maximum number of significant digits to be printed. For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered. For c type: it has no effect. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed. The precision is not specified in the format string, but as an additional .* integer value argument preceding the argument that has to be formatted.

lengt h h l

description The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, xand X). The argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string

for specifiers c and s. The argument is interpreted as a long double (only applies to floating point specifiers: e, E, f, g and G).

additional arguments The function expects a sequence of references as additional arguments, each one pointing to an object of the type specified by their corresponding %-tag within the format string, in the same order. For each format specifier in the format string that retrieves data, an additional argument should be specified. These arguments are expected to be references (pointers): if you want to store the result of a sscanf operation on a regular variable you should precede its identifier with the reference operator, i.e. an ampersand sign (&), like in: int n; sscanf (str,"%d",&n);

You might also like