Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
C String Tokenizer 
 

In this section, you will learn how to use strtok() function to break the string into a series of tokens.

 

C String Tokenizer

                         

In this section, you will learn how to use strtok() function to break the string into a series of tokens. You can see in the given example, we have define a string and a pointer. The expression ch = strtok (st," ") extract the string from the string sequence one by one and printf ("%s\n", ch) prints the string after being tokenized. The expression ch = strtok (NULL, " ,") extract the whole string from the string sequence till the ch get equal to null.

Here is the code:

 

 

 

 

TOKENIZE.C

#include <stdio.h>
#include <conio.h>
#include <string.h>
int main() {
  char st[] ="Where there is will, there is a way.";
  char *ch;
  clrscr();
  printf("Split \"%s\"\n", st);
  ch = strtok(st, " ");
  while (ch != NULL) {
    printf("%s\n", ch);
    ch = strtok(NULL, " ,");
  }
  getch();
  return 0;
}

Output will be displayed as:

TOKENIZE.EXE

Download Source Code:

                         

» View all related tutorials
Related Tags: c ide function language fun io pointer vi int id address define poi unc add to point attach feature base

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

# include <iostream>
#include <cstdlib>
#include <string.h>

using namespace std;
int main(int argc, char* argv[])
{
string str;

cout<<"Input String "<<"\n";
getline(cin,str);
cout<<"String:"<<str<<endl;
cout<<";;;;;;;;;;;;;;;;;;"<<endl;
string com_rec= str.substr(0, 2);
string end = str.substr(2);
cout<<com_rec<<"\n";
cout<<end<<"\n";
string sou = end.substr(end.find_first_of(' '),end.find_last_of(' '));
cout<<"Source........."<<sou<<endl;
string des = end.substr(end.find_last_of(' '),end.length());
cout<<"Destination........."<<des<<endl;





}

Posted by Kaml Hyder on Friday, 04.10.09 @ 20:13pm | #86718

# include <iostream>
#include <cstdlib>
#include <string.h>

using namespace std;
int main(int argc, char* argv[])
{
string str;

cout<<"Input String "<<"\n";
getline(cin,str);
cout<<"String:"<<str<<endl;
cout<<";;;;;;;;;;;;;;;;;;"<<endl;
string com_rec= str.substr(0, 2);
string end = str.substr(2);
cout<<com_rec<<"\n";
cout<<end<<"\n";
string sou = end.substr(end.find_first_of(' '),end.find_last_of(' '));
cout<<"Source........."<<sou<<endl;
string des = end.substr(end.find_last_of(' '),end.length());
cout<<"Destination........."<<des<<endl;





}

Posted by Kaml Hyder on Friday, 04.10.09 @ 20:09pm | #86717

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.