Sort by

recency

|

744 Discussions

|

  • + 0 comments

    include

    include

    include

    include

    int main() {

    char *s;
    s = malloc(1024 * sizeof(char));
    scanf("%[^\n]", s);
    s = realloc(s, strlen(s) + 1);
    //Write your logic to print the tokens of the sentence here.
    
    // printf("%d", strlen(s))
    
    int i = 0;
    while(i < strlen(s)){
        if(s[i] == ' '){
            printf("\n");
            i++;
        }
        else{
            printf("%c", s[i]);
            i++;
        }
    }
    
    
    return 0;
    

    }

  • + 0 comments
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main() {
    
        char *s;
        s = malloc(1024 * sizeof(char));
        scanf("%[^\n]", s);
        s = realloc(s, strlen(s) + 1);
        
        for(int i = 0; i < strlen(s); i++) {
            if(s[i] == '\x20'){ // ASCII for space is \40, hex for \40 is \x20
                printf("\n");
                continue;
            }
            printf("%c", s[i]);
        }
    
        return 0;
    }
    
  • + 0 comments

    If you have no idea how to approach this, check out the example code here:

    https://en.cppreference.com/w/c/string/byte/strtok

  • + 0 comments

    Here is Printing Tokens in C problem solution - https://programmingoneonone.com/hackerrank-printing-tokens-solution-in-c.html

  • + 0 comments

    include

    include

    include

    include

    int main() {

    char *s;
    s = malloc(1024 * sizeof(char));
    scanf("%[^\n]", s);
    s = realloc(s, strlen(s) + 1);
    //Write your logic to print the tokens of the sentence here.
    

    char *word = strtok(s, " "); while(word != NULL) { printf("%s\n", word); // Print each word in new line word = strtok(NULL, " "); // Move to next word } return 0; }