🔒 Closed C masters patulong: Linked Lists: FREE LOAD

Status
Not open for further replies.

pineng

Eternal Poster
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.
 
IMG_20200308_180101.webp
 
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.

About this Thread

  • 16
    Replies
  • 1K
    Views
  • 3
    Participants
Last reply from:
pineng

Online now

Members online
1,017
Guests online
737
Total visitors
1,754

Forum statistics

Threads
2,276,972
Posts
28,973,435
Members
1,229,673
Latest member
saikonkhan134153
Back
Top