Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- int main(){
- char str[100] = "This is sample sentence";
- const char s[2] = " ";
- char* token;
- token = strtok(str, s);
- while(token != NULL){
- printf("%s\n", token);
- token = strtok(NULL, s);
- }
- return 0;
- }
- -------------------- with out strtok ------------
- #include <stdio.h>
- #include <stdbool.h>
- #include <ctype.h>
- static bool readLine (const char* data, const char** beginPtr, const char** endPtr) {
- static const char* nextStart;
- if (data) {
- nextStart = data;
- return true;
- }
- if (*nextStart == '\0') return false;
- *beginPtr = nextStart;
- // Find next delimiter.
- do {
- nextStart++;
- } while (*nextStart != '\0' && *nextStart != ' ');
- // Trim whitespace.
- *endPtr = nextStart - 1;
- while (isspace(**beginPtr) && *beginPtr < *endPtr)
- (*beginPtr)++;
- while (isspace(**endPtr) && *endPtr >= *beginPtr)
- (*endPtr)--;
- (*endPtr)++;
- return true;
- }
- int main (void) {
- const char* data = "This is sample sentence";
- const char* begin;
- const char* end;
- readLine(data, 0, 0);
- while (readLine(0, &begin, &end)) {
- printf("%.*s\n", end - begin, begin);
- }
- return 0;
- }
- ----------------- Delimiting-------------
- #include <stdio.h>
- #include <string.h>
- int main ()
- {
- char str[] ="- This, a sample string.";
- char * pch;
- printf ("Splitting string \"%s\" into tokens:\n",str);
- pch = strtok (str," ,.-");
- while (pch != NULL)
- {
- printf ("%s\n",pch);
- pch = strtok (NULL, " ,.-");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement