Skip to content
Programming101
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone
Printing tokens problem solution in c

HackerRank Printing Tokens solution in C

YASH PAL, 17 July 202414 June 2025

In this tutorial, we are going to solve the HackerRank printing tokens problem in c programming with practical program code example and step-by-step explanation. in this problem, we need to take a sentence as input that length will always be less than 1000 characters and we need to print each word of the sentence in a new line.

Printing tokens problem solution in c
Printing tokens in c

Solution in c programming

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    char *s;
    s = malloc(1024 * sizeof(char));
    scanf("%[^\n]", s);
    s = realloc(s, strlen(s) + 1);
    for (char *c = s; *c != NULL; c++) {
    if (*c == ' ') {
        *c = '\n';
    }
}
printf("%s", s);
    return 0;
}
Printing tokens in c

Second solution

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *p;
    p = malloc(1024 * sizeof(char));
    scanf("%[^\n]", p);
    p = realloc(s, strlen(p) + 1);
    int len = strlen(p);
    for(int i = 0; i < len; i++) {
        if(p[i] == ' ') {
            printf("\n");
        }
        else {
            printf("%c", p[i]);
        }
    }
    free(p);
    return 0;
}
C Solutions coding problems solutions Hackerrank Problems Solutions cHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes