What's new

Closed C masters patulong: Linked Lists: FREE LOAD

Status
Not open for further replies.

pineng

Forum Veteran
Joined
May 31, 2017
Posts
1,228
Solutions
1
Reaction
2,017
Points
577
Patulong po mga master sa C programming:

Given a linked list:
Here I am

H-e-r-e-(space)-I-(space)-a-m-

: Count the number of words in a linked list.
Dapat ang sagot po jan ay 3.
Hmmm, may idea po ba kayo jan kung paano maiimplement?

TIA

Loadan ko po ng 50 makakasagot hehe.
 
Last edited:
IMG_20200308_180101.jpg
 

Attachments

nakagawa ako 2 different files, isang simple lang, nung sa screenshot tapos yung isa nadebug pag maraming space kunyari i __ am ___here (yung underscore ay space, for representation lang), magiging 3 pa rin bilang kahit gano karaming spaces basta may words
 
Easy:

C:
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    size_t n = 2;
    char* mybuffer = malloc(sizeof(char) + n);
    printf("Enter string: ");
    size_t size = getline(&mybuffer, &n, stdin);
    int count = 0;
     for (int i=0; i<size; i++) {
         if ((mybuffer[i] == ' ' && mybuffer[i+1] != ' ') || mybuffer[i] == '\n') {
             ++count;
         }
     }
     free(mybuffer);
     printf("Num of words: %d\n", count);
     return 0;
}
 
Easy:

C:
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    size_t n = 2;
    char* mybuffer = malloc(sizeof(char) + n);
    printf("Enter string: ");
    size_t size = getline(&mybuffer, &n, stdin);
    int count = 0;
     for (int i=0; i<size; i++) {
         if ((mybuffer[i] == ' ' && mybuffer[i+1] != ' ') || mybuffer[i] == '\n') {
             ++count;
         }
     }
     free(mybuffer);
     printf("Num of words: %d\n", count);
     return 0;
}
linked list ba to
 
Status
Not open for further replies.
Back
Top