- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to print string tokens
Suppose we have a string s that contains a sentence with few words. We shall have to print each word into new lines. To do this we can use the strtok() function under the string.h header file. This function takes the string and a delimiter. Here the delimiter is blank space " ".
So, if the input is like s = "Let us see some string tokenizing fun", then the output will be
Let us see some string tokenizing fun
To solve this, we will follow these steps −
token := first word by using strtok(s, " ") here delimiter is " "
while token is non-zero, do:
display token
token := next token of s, from now pass NULL as first argument of strtok with same delimiter space " ".
Example
Let us see the following implementation to get better understanding −
#include <stdio.h> #include <string.h> int main(){ char s[] = "Let us see some string tokenizing fun"; char* token = strtok(s, " "); while (token) { printf("%s
", token); token = strtok(NULL, " "); } }
Input
Let us see some string tokenizing fun
Output
Let us see some string tokenizing fun
- Related Articles
- C program to detect tokens in a C program
- Print shortest path to print a string on screen in C Program.
- C/C++ Tokens?
- Java Program to Print a String
- Kotlin Program to Print a String
- C program to print a string without any quote in the program
- C Program to print all permutations of a given string
- C program to print the ASCII values in a string.
- Tokens in C
- Program to print all substrings of a given string in C++
- Substitute tokens in a String in Java
- How to split a Java String into tokens with StringTokenizer?
- How to Print a String in Swift Program?
- Explain C tokens in C Language
- Bag of Tokens in C++

Advertisements