• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Split String in C programming using strtok_r() | Parsing CSV File

Aniruddha Chaudhari/16545/0
C / C++Code

For example, you have a string sentence “CSEstack is a programming portal”. You want to get each word from the sentence like “CSEstack”, “is”, “a”, “programming” and “portal.”. This can be done by splitting the sentence and storing them.

You can use function strtok_r() to split the sentence in C/C++ programming.

About function  strtok_r() in C/C++

Input parameter to the strtok_r() function:

  • a string which you want to split
  • delimiter to distinguish and split the string
  • address of the sentence you want to split

How does it work?

strtok_r() returns the token pointing to the next substring after delimiter. Calling function strtok_r() in a loop will give us all the sub-strings after splitting.

Split String in C Programming using strtok_r()

Check the below program.

// C program to demonstrate working of strtok_r() 
// by splitting string based on space character. 
#include <stdio.h> 
#include <string.h> 

int main() 
{ 
   char strToSplit[] = "CSEstack is a programming portal";

   char *token;
   int i=0;
   char *strArr[5] = {0};
   char* strSplit = strToSplit;
   while ((token = strtok_r(strSplit , " ", &strSplit)))
       strArr[i++] = token;

   for(int i=0; i<5; i++)
       printf("\n ++i) %s",strArr[i]);
}

In the above program, the delimiter is space ” “.  So, The string between two spaces will be considered as one token.

Output:

1) CSEstack
2) is
3) a
4) programming
5) portal

After splitting the string, you can also store the data in the array.

Where you can use strtok_r()?

Use Cases:

  • If you are working on any networking project, you can concatenate all the payload. Send it to another device and split the payload.
  • In networking, you can also use this function to split the IP address.
  • It can also be useful for sending the grouped data using CGI variable.

Using strtok_r() for Parsing CSV file

CSV (comma separated value) is a special type of data structure where we can save the different data entities separated by a comma.

Example:

Name,Age,profession
Steve,34,developer
Noob,55,tester
Clave,32,designer

You can simply read the CSV file and loop over each line to get the name, age and profession of the employee by using strtok_r(). Here, delimiter will be ‘,’ instead of space.

// C program to demonstrate working of strtok_r() 
// by splitting string based on space character. 
#include <stdio.h> 
#include <string.h> 

int main() 
{ 
   char strEmpData[] = "Steve,34,developer";

   char *token;
   int i=0;
   char *strArr[3] = {0};
   char* strSplit = strEmpData;
   while ((token = strtok_r(strSplit , ",", &strSplit)))
       strArr[i++] = token;
 
  printf("\n Name: %s",strArr[0]);
  printf("\n Age: %d",atoi(strArr[1]));
  printf("\n Profession: %s",strArr[2]);
}

Output:

Name: Steve
Age: 34
Profession: developer

Related String Program in C:

  • Difference Between Array and String in Programming
  • C Program to Remove White Spaces from String

Any question or point to discuss this code to split String in C programming, write in the comment section.

c-string
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard