Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Printing Tokens solution in C

YASH PAL, 17 July 202417 July 2024

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 coding problems hackerrank solutions cHackerRank

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes