echo in C

Discussion in 'Programming General' started by Faskist, Apr 1, 2008.

echo in C
  1. Unread #1 - Apr 1, 2008 at 7:39 PM
  2. Faskist
    Joined:
    Apr 25, 2005
    Posts:
    1,869
    Referrals:
    0
    Sythe Gold:
    0

    Faskist Tuxhead
    Banned

    echo in C

    Code:
    #include <stdio.h> 
    int main(int argc, char *argv[]) {
      int i;
      for(i=1;i<argc;i++) {
        printf("%s",&argv[i][0]);
        if(!(i+1==argc)) {
          printf(" ");
        }
      }
      printf("\n");
    }
    Written by me and Fouisgras over MSN :3

    Can take an arbitrary number of arguments, but doesn't die on 0 args (Just prints newline, which is sad). Any help on detecting a 0 number of arguments would be much appreciated.
     
  3. Unread #2 - Apr 1, 2008 at 7:41 PM
  4. Fouisgras
    Joined:
    Jan 21, 2007
    Posts:
    2,202
    Referrals:
    2
    Sythe Gold:
    0

    Fouisgras Jansen's Lover
    $5 USD Donor

    echo in C

    :3 horray.
     
  5. Unread #3 - Apr 2, 2008 at 5:11 AM
  6. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    echo in C

    So it practically just prints out the command line arguments?
     
  7. Unread #4 - Apr 2, 2008 at 8:41 AM
  8. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    echo in C

    Code:
    #include <stdio.h> 
    #include <ctype.h>
    int main(int argc, char *argv[]) {
      int i;
      if(argv[1]!=NULL) { // If there are arguments
        for(i=1;i<argc;i++) {
          printf("%s ",&argv[i][0]); // Loop through the arguments printing them
        }
      }
      else { // If no arguments
        for(i=0;i<sizeof(argv[0]);i++) {
          argv[0][i] = toupper(argv[0][i]); // Take the [0] argument which will be the name of the command and make it uppercase
        }
        printf("%s is on.\n", argv[0]); // Print out "<name of command> is on" just as echo does when there are no arguments
      }
    }
    Made a couple minor changes and added detection for zero arguments like the real 'echo' has. Also, commented it but I think that's mostly to help out Fouis. ;)
     
  9. Unread #5 - Apr 2, 2008 at 6:10 PM
  10. Fouisgras
    Joined:
    Jan 21, 2007
    Posts:
    2,202
    Referrals:
    2
    Sythe Gold:
    0

    Fouisgras Jansen's Lover
    $5 USD Donor

    echo in C

    Thanks null :D
     
  11. Unread #6 - Apr 2, 2008 at 6:12 PM
  12. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    echo in C

    UNIX and Windows both already have an echo command... >_>
     
  13. Unread #7 - Apr 2, 2008 at 7:36 PM
  14. Fouisgras
    Joined:
    Jan 21, 2007
    Posts:
    2,202
    Referrals:
    2
    Sythe Gold:
    0

    Fouisgras Jansen's Lover
    $5 USD Donor

    echo in C

    Yet inefficient. Ours is smaller. Less bloated is always win.

    Anyway it wasn't meant to be fully function. It was mainly an excercise.

    GTFO :(
     
  15. Unread #8 - Apr 2, 2008 at 7:37 PM
  16. Faskist
    Joined:
    Apr 25, 2005
    Posts:
    1,869
    Referrals:
    0
    Sythe Gold:
    0

    Faskist Tuxhead
    Banned

    echo in C

    We know :3 But ours is small and sexy, and the GNU one is massive.
     
< [BeginnerTut] Making a timer countdown | A few simple questions. :) >

Users viewing this thread
1 guest


 
 
Adblock breaks this site