Jump to content
Flubber

[C] Word counting (K&R)

Recommended Posts

OK, m-am straduit, dar nu am reusit decat in cele din urma sa fiu din ce in ce mai nervos, frustrat si foarte dezamagit din cauza ca nu am reusit sa fac niste exercitii din ultimele subpuncte al primului capitol din K&R 2nd edition, am sa iau unul din exercitii si sper sa va puna mintea la lucru asa cum am patit-o si eu.

Exercitiul este urmatorul:

Va este prezentat codul de mai jos, va este explicat ce face, descompus etc. Pana acum aveti experienta doar cu codul de mai jos, nu stiti alte librarii in afara de <stdio.h> sau ceva foarte fancy.

Dupa ce vi s-a explicat codul si l-ati studiat, va este cerut sa scrieti (tot in C) un program ce ia cuvintele prin `getchar();` si le printeaza prin `putchar();` cu o conditie. Conditia este ca fiecare cuvant (ex: gigel, laurica etc.) sa fie printat pe o linie noua.

Exemplu:

input


Ana are mere.

output


Ana
are
mere.

Postati codul scris de voi. O rezolvare la acest exercitiu se afla la urmatoarea adresa

* SPOILER *
Pentru a vedea rezolvarea la acest exercitiu, click >>>> [URL="http://www.trunix.org/programlama/c/kandr2/krx112.html"]aici[/URL].


/* Word counting in C, urmatorul exemplu
* este cel extras din cartea K&R 2nd ed-
* -ition, aici ne este prezentat un prog-
* -ram ce numara anumite caractere */


#include <stdio.h>

#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */

main() {
int c, nl, nw, nc, state; /* we declare variables 'character, newline, newword, newcharacter, state' as integers */

state = OUT; /* we declare state to have value of 0 (outside a word) */
nl = nw = nc = 0; /* we initialize main counting variables as 0 */

while ((c = getchar()) != EOF) { /* while character stored in c is not EOF do the following */
++nc; /* increment counter `new character` - one character by getchar() has been taken in input */
if (c == '\n') { /* if c is newline ... */
++nl; /* ... increment `new line` counter */
}
if (c == ' ' || c == '\n' || c == '\t') { /* if c is blank, or newline or tab ... */
state = OUT; /* mark state as OUTSIDE of a word */
}
else if (state == OUT) { /* else if state is OUT ... */
state = IN; /* set state as IN because we are `inside` a word */
++nw; /* and increment counter `new word` */
}
}
printf("%d %d %d\n", nl, nw, nc); /* print on screen data stored of nl, nw, nc */
}


/* Dupa ce ne sunt explicati pasii, ni se da un exercitiu
* ce ne cere a face un program. Acest program are sarcina
* de a lua input-ul user-ului si a il printa pe ecran, dar
* cu o regula, fiecare cuvant trebuie sa fie pe o linie noua
* care ar fi codul scris de tine? Avand in vedere ca stii cam
* ce este prezentat sus, nimic fancy, vreo librarie smechera
* ce iti permite ghidusii ca string replace & so on */

Sau Codepad: C code - 41 lines - codepad

Edited by Flubber
update pentru clarificare
Link to comment
Share on other sites

#include <stdio.h>
#define N 1024

int main()
{
char words[N], *it;
fputs("Data: ", stdout);
fgets(words, N, stdin);
for (it = words; *it; ++it) {
if (*it == ' ' || *it == '\n') {
putchar('\n');
} else {
putchar(*it);
}
}
return 0;
}

Edited by cmiN
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...