Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 13th, 2004, 6:03 AM   #1
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
i've made my own copy of the tool "echo", this is not to improve it or anything, but only to learn about strings and pointers...
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>

int main(int argc,char** argv[])
{
  if(argc>1)
  {
    char *str[100];/*one array for every argument*/
    char *str2=" ";
    int loop=0;
    int count=1;
    
    while (loop<=argc)/*loop to copy every argument to a string[]*/
    {
      str[loop]=argv[count];
      loop++;
      count++;
    }
      
    for(loop=0;loop<argc-1;loop++)/*loop to print the strings with a space between them(just like they were entered)*/
    {
      printf("%s",str[loop]);
      printf("%s",str2);
    }    
    return 0;
  }
  return EXIT_FAILURE;
}
now the question: what can i do to improve this code? isn't there a possibility to read argv[1] as one complete string, even if a space is entered?
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Old Aug 13th, 2004, 8:15 AM   #2
Kylixen
Programmer
 
Kylixen's Avatar
 
Join Date: Jun 2004
Location: SC
Posts: 60
Rep Power: 5 Kylixen is on a distinguished road
Send a message via AIM to Kylixen Send a message via MSN to Kylixen
Quote:
Originally posted by lepricaun@Aug 13 2004, 11:03 AM
i've made my own copy of the tool "echo", this is not to improve it or anything, but only to learn about strings and pointers...
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>

int main(int argc,char** argv[])
{
  if(argc>1)
  {
    char *str[100];/*one array for every argument*/
    char *str2=" ";
    int loop=0;
    int count=1;
    
    while (loop<=argc)/*loop to copy every argument to a string[]*/
    {
      str[loop]=argv[count];
      loop++;
      count++;
    }
      
    for(loop=0;loop<argc-1;loop++)/*loop to print the strings with a space between them(just like they were entered)*/
    {
      printf("%s",str[loop]);
      printf("%s",str2);
    }    
    return 0;
  }
  return EXIT_FAILURE;
}
now the question: what can i do to improve this code? isn't there a possibility to read argv[1] as one complete string, even if a space is entered?
#include <stdio.h>

int main(int argc, char *argv[]){

  int i;

  for(i = 1;i < argc;i++){

    printf("%s", argv[i]);

  }

  printf("\n");

}
__________________
ALLOW IMAGES IN SIGNATURES NOW
Kylixen is offline   Reply With Quote
Old Aug 13th, 2004, 8:59 AM   #3
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
Quote:
CODE
#include <stdio.h>

int main(int argc, char *argv[]){

int i;

for(i = 1;i < argc;i++){

printf("%s", argv[i]);

}

printf("\n");

}
yes, stupid me, this would be enough too!
only thing:
printf("%s", argv[i]);

i would change into :
printf("%s ", argv[i]);

cause then the problem of the space is solved...

great, guess i didn't had to make it this difficult
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Old Aug 13th, 2004, 1:39 PM   #4
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
Since no one else fielded the post increment operators on loop and count

count++;
loop++;

I will field that.

As it ends up post increment operators are actually very inefficient. You chould have used the following code:

    while (loop<=argc)/*loop to copy every argument to a string[]*/
    {
      str[loop++]=argv[count++];
    }

If you are using a post-increment operator on an operand in which the pre-incremented value is no being used in the same line, then you are wasting memory and time since a post increment operator will actually instantiate a copy of the variable, increment the original and return the copy.. kind of like this: (I will use a template so that I can demonstrate the generic use of the operator when showing my point)

//post increment operator...
template< classname typeT >
typeT operator++( typeT &lhv, typeT )
{
   typeT temp = lhv;
   lhv += 1;
   return temp;
}

//preincrement operator
template< classname typeT >
typeT &operator++( typeT &rhv )
{
   rhv += 1;
   return rhv;
}

Also as you can see due to the nature of the post and pre increment operators a second parameter (which fortunatly does not have to even be initialized) must be present for a post increment.
__________________
Clifford Matthew Roche &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Aug 14th, 2004, 8:28 AM   #5
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
see, there's the difference between me (newbie) and you (proffesional programmer), i don't understand what the f**k you are talking about

but eventually i will, i just have to practice/learn and be patient.

( i just started programming 2 months ago )
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Old Aug 15th, 2004, 5:46 PM   #6
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
I fyou do a google such for the C++ FAQ Lite, you will actually find that point, and a few other very good points covered which touch on topics of robustness and optimization, perhaps it could explain that a little bit more.

You will eventually though come to a point where you will consider all points such as that when tackling a project No worries..
__________________
Clifford Matthew Roche &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Aug 16th, 2004, 4:17 AM   #7
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
thanks, i will do that
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:03 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC