:: Re: [DNG] Studying C as told. (For …
Page principale
Supprimer ce message
Répondre à ce message
Auteur: Edward Bartolo
Date:  
À: dng
Sujet: Re: [DNG] Studying C as told. (For help)
Ooops, this is the corrected program for exercise1-21

#include <stdio.h>

/* insert tabs instead of repeated spaces
* I am assuming a tab is equivalent to 3 spaces and forgetting tabs
that are defined at specific columns */

int tab_width = 3;

void replace_spaces_with_tabs(char c) {
  int count = 0;
  if (c == ' ') {
    ++count;


    while((c = getchar()) == ' ') {
      ++count;
      if (count == tab_width) {
        putchar('\t');
        count = 0;
      } else continue;


      if (count < tab_width && count > 0)
        while(putchar(' ') && --count);
    }
  }


putchar(c);
}

int main() {
  char c;
  while((c = getchar()) != EOF) {
    replace_spaces_with_tabs(c);
  }


return 0;
}

On 23/06/2016, Edward Bartolo <edbarx@???> wrote:
> Hi,
>
> I am doing exercise 1-21 Pg 48 The C programming language" (Kernighan
> & Ritchie). Since I am finding difficulty inspecting tabs in XFCE4.10
> terminal, I would like someone to inspect this program which replaces
> repeating spaces with tabs when there are sufficient consecutive
> spaces. I am not asking for a long reply. A short reply giving me
> feedback as to whether the code is good or not, is enough.
>
> exercise 1-21
> -------------------------------
>
> #include <stdio.h>
>
> /* insert tabs instead of repeated spaces */
>
> int tab_width = 3;
>
> void replace_spaces_with_tabs(char c) {
>   int count = 0;
>   if (c == ' ') {
>     ++count;

>
>     while((c = getchar()) == ' ') {
>       ++count;
>       if (count == tab_width) {
>         putchar('\t');
>         count = 0;
>       }

>
>       if (count < tab_width && count > 0)
>         while(putchar(' ') && --count);
>     }
>   } else putchar(c);
> }

>
> int main() {
>   char c;
>   while((c = getchar()) != EOF) {
>     replace_spaces_with_tabs(c);
>   } else putchar(c);

>
> return 0;
> }
>
> --------------------------------------
>
>
> Edward
>
> On 23/06/2016, Edward Bartolo <edbarx@???> wrote:
>> Hi,
>>
>> At this point of my C studies, I did a test to get feedback of my C
>> language learning progress. I found an online C test which consisted
>> of 20 questions and a time duration of 30 minutes.
>>
>> Link: http://www.indiabix.com/online-test/c-programming-test/random
>>
>> The test included concepts and syntax constructs that I haven´t yet
>> covered in "The C programming language" (Kernighan & Ritchie). My
>> score was 12/20 and oddly enough I scored the two most difficult
>> questions correct while making mistakes in trivialities. :(
>>
>> I will give the test a second try to make sure my learning progress is
>> real.
>>
>> Edward
>>
>