Wednesday, October 21, 2020

22-10- HACKERRANK C PROGRAMMING - PRINTING TOKENS

 22-10- HACKERRANK C PROGRAMMING - PRINTING TOKENS

PROBLEM

SOLUTION- 

MY SOLUTION

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

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


PROBLEM SETTERS SOLUTION
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

No comments:

Post a Comment

LEETCODE JUNE 2021 DAILY CODING CHALLENGE

 LEETCODE JUNE 2021 DAILY CODING CHALLENGE JUNE 1 JUNE 2 JUNE 3 JUNE 4 JUNE 5 Maximum Performance of a Team LINK You are given two integers ...