Thread: Why is this program not doing what I want it to do.

  1. #1
    Registered User
    Join Date
    May 2024
    Posts
    1

    Why is this program not doing what I want it to do.

    Hello, this is my first post here, There is no noob section and I don't know if I can post it here, please don't ban me.

    This program is supposed to, if more than one space is found, only print one and then continue printing the line and do the same for any other spaces that are more than one.

    Code:
    #include <stdio.h>
    
    int getandshort(char store[], char hold[]);
    int nullit(char arr[]);
    int main()
    {
    int i;
    char storeline[100];
    char holdspace[100];
    nullit(storeline);
    nullit(holdspace);
    
    getandshort(storeline, holdspace);
    
    for(i = 0; i < 100; ++i)
            printf("%c", storeline[i]);
    return 0;
    }
    
    
    int getandshort(char store[], char hold[])
    {
    int i;
    int c;
    for(i = 0; (c = getchar()) != '\n' && c != EOF; ++i)
    {
            if(c != ' ' || c == ' ' && store[i - 1] != ' ')
                    store[i] = c;
    }
    store[i] = '\0';
    }
    
    int nullit(char arr[])
    {
    int i;
      for(i = 0; i < 100; ++i)
      {
        arr[i] = '\0';
      }
    }
    When I run it, it shortens the trail of spaces but I think it only removes one.
    What's up with this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    First, indentation makes for much easier code reading.
    Code:
    #include <stdio.h>
    
    int getandshort(char store[], char hold[]);
    int nullit(char arr[]);
    int main()
    {
      int i;
      char storeline[100];
      char holdspace[100];
    
      nullit(storeline);
      nullit(holdspace);
      getandshort(storeline, holdspace);
    
      for (i = 0; i < 100; ++i)
        printf("%c", storeline[i]);
      return 0;
    }
    
    int getandshort(char store[], char hold[])
    {
      int i;
      int c;
      for (i = 0; (c = getchar()) != '\n' && c != EOF; ++i) {
        if (c != ' ' || c == ' ' && store[i - 1] != ' ')
          store[i] = c;
      }
      store[i] = '\0';
    }
    
    int nullit(char arr[])
    {
      int i;
      for (i = 0; i < 100; ++i) {
        arr[i] = '\0';
      }
    }
    Second, turn up the warnings.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘getandshort’:
    foo.c:25:30: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
       25 |     if (c != ' ' || c == ' ' && store[i - 1] != ' ')
          |                     ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
    foo.c:29:1: warning: control reaches end of non-void function [-Wreturn-type]
       29 | }
          | ^
    foo.c: In function ‘nullit’:
    foo.c:37:1: warning: control reaches end of non-void function [-Wreturn-type]
       37 | }
          | ^
    - both your functions could be declared void rather than int
    - adding ( ) helps to make sense of your complicated logic.

    The two problems are:
    1. store[i-1] is off the end of the array when i = 0
    2. You increment i for every character, not every valid character.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    143
    In your for-loop, you always increment "i". But if a repeated-space is seen, you should not increment "i" because you are not keeping the second space.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-25-2020, 05:06 PM
  2. Replies: 7
    Last Post: 03-15-2016, 06:35 AM
  3. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  4. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  5. Replies: 1
    Last Post: 03-03-2009, 04:47 PM

Tags for this Thread