a little mistake

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ash

    a little mistake

    i am writing this program (for exercise1-9 in k&r-2nd edition) which
    removes extra spaces in string
    example- "test string" will be "test string"

    #include<stdio. h>
    #include<string .h>
    #include<conio. h>
    main()
    {
    char str[20],st[20];
    int i=0,j=0;
    printf("enter string");
    gets(str);

    while(str[i]!=NULL)
    {
    if(str[i]==' ')
    { st[j++]=st[i++];

    while(str[i]==' ') i++;

    }
    else
    st[j++]=str[i++];
    }
    st[j]=NULL;
    strcpy(str,st);
    puts(str);
    }

    (compiled in turbo c++ for windows version 3.1)
    i know there should be other ways to write this program (thats a bad
    program ),can someone help me in writing that program in compact way
    and give me some logic and tips.
    i want to use gcc but i don`t have linux operating system and one
    software which allows gcc compiling in windows (cygwin) is not working.
    anyone can advice me such a program that allows compiling with gcc in
    windows( this is off topic but advices are always welcome).

    thankx

  • Malcolm

    #2
    Re: a little mistake




    "ash" <ashishmourya21 @rediffmail.com > wrote in message
    news:1150482182 .254438.295290@ i40g2000cwc.goo glegroups.com.. .[color=blue]
    >i am writing this program (for exercise1-9 in k&r-2nd edition) which
    > removes extra spaces in string
    > example- "test string" will be "test string"
    >
    > #include<stdio. h>
    > #include<string .h>
    > #include<conio. h>
    > main()
    > {
    > char str[20],st[20];
    > int i=0,j=0;
    > printf("enter string");
    > gets(str);
    >
    > while(str[i]!=NULL)
    > {
    > if(str[i]==' ')
    > { st[j++]=st[i++];
    >
    > while(str[i]==' ') i++;
    >
    > }
    > else
    > st[j++]=str[i++];
    > }
    > st[j]=NULL;
    > strcpy(str,st);
    > puts(str);
    > }
    >
    > (compiled in turbo c++ for windows version 3.1)
    > i know there should be other ways to write this program (thats a bad
    > program ),can someone help me in writing that program in compact way
    > and give me some logic and tips.
    > i want to use gcc but i don`t have linux operating system and one
    > software which allows gcc compiling in windows (cygwin) is not working.
    > anyone can advice me such a program that allows compiling with gcc in
    > windows( this is off topic but advices are always welcome).
    >
    > thankx
    >[/color]
    Separate out ewhat you are doing into a function.

    In this case you want

    /*
    turns whitespace spans into single spaces
    (and trims leading / trailing white space?)
    */
    void onespace(char *out, char *in)

    Decide if you want to allow out and in to be the same. This will make your
    function
    more convenient to use, but you might have to be a little bit more careful
    in coding it.

    Basically you maintain two pointer, a "write" pointer and a "read" pointer.
    If you hit
    a non-whitespace you write to the "write" pointer and increment both.
    If you hit a whitespace character, you write a single space to the "write"
    pointer. Then you increment the "read" pointer whilst it is still pointing
    to white space.
    --
    Buy my book 12 Common Atheist Arguments (refuted)
    $1.25 download or $7.20 paper, available www.lulu.com/bgy1mm


    Comment

    • santosh

      #3
      Re: a little mistake

      ash wrote:
      .... snip ...[color=blue]
      > i want to use gcc but i don`t have linux operating system and one
      > software which allows gcc compiling in windows (cygwin) is not working.
      > anyone can advice me such a program that allows compiling with gcc in
      > windows( this is off topic but advices are always welcome).[/color]

      Generally Cygwin is a bit complex to set up and use. Use MinGW instead.
      It uses native Win32 ports of the GNU toolchain.



      Comment

      • Keith Thompson

        #4
        Re: a little mistake

        "ash" <ashishmourya21 @rediffmail.com > writes:[color=blue]
        > i am writing this program (for exercise1-9 in k&r-2nd edition) which
        > removes extra spaces in string
        > example- "test string" will be "test string"
        >
        > #include<stdio. h>
        > #include<string .h>
        > #include<conio. h>[/color]

        <conio.h> is not a standard header. You don't use it anyway. Delete
        the above line.

        I find it clearer to leave a space between "include" and "<".
        [color=blue]
        > main()[/color]

        int main(void)
        [color=blue]
        > {
        > char str[20],st[20];
        > int i=0,j=0;
        > printf("enter string");[/color]

        Since this doesn't end in a newline, it's not guaranteed to appear.
        Add "fflush(stdout) ;".

        I'd add a blank, or perhaps ": ", at the end of the prompt; it looks
        better. Without it, if I type "foo", I see "enter stringfoo" on the
        screen.
        [color=blue]
        > gets(str);[/color]

        Never use gets(). Never use gets(). Never use gets().

        See question 12.23 in the comp.lang.c FAQ, <http://www.c-faq.com/>.
        [color=blue]
        > while(str[i]!=NULL)[/color]

        NULL is a null pointer constant. You're trying to compare a character
        value to a pointer value. It happens to work on your system for
        obscure reasons I won't get into, but it's bad style at the very
        least. The way to represent a null character is '\0'.
        [color=blue]
        > {
        > if(str[i]==' ')
        > { st[j++]=st[i++];[/color]

        I think you mean "st[j++]=st[i++];"
        [color=blue]
        >
        > while(str[i]==' ') i++;
        >
        > }
        > else
        > st[j++]=str[i++];
        > }
        > st[j]=NULL;[/color]

        Again, use '\0', not NULL.
        [color=blue]
        > strcpy(str,st);
        > puts(str);[/color]

        return 0;
        [color=blue]
        > }[/color]

        And don't underestimate the importance of consistent formatting,
        particularly indentation.

        Apart from that, the program doesn't seem unreasonable.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        • Default User

          #5
          Re: a little mistake

          ash wrote:
          [color=blue]
          > i am writing this program (for exercise1-9 in k&r-2nd edition) which
          > removes extra spaces in string
          > example- "test string" will be "test string"[/color]

          That's not the actual exercise:

          Exercise 1-9. Write a program to copy its input to its output,
          replacing each string of one or more blanks by a single blank.

          [color=blue]
          >
          > #include<stdio. h>
          > #include<string .h>
          > #include<conio. h>[/color]

          <conio.h> is not a standard header. You should not use it, as it's not
          very portable.
          [color=blue]
          > main()[/color]

          Use the following form:

          int main(void)
          [color=blue]
          > {
          > char str[20],st[20];[/color]

          These fixed buffers are likely a problem.
          [color=blue]
          > int i=0,j=0;
          > printf("enter string");
          > gets(str);[/color]

          Never, ever use gets(). It's dangerous. What happens when someone
          enters a string with 50 characters? Bad things.
          [color=blue]
          >
          > while(str[i]!=NULL)[/color]

          NULL is a null pointer constant. It might be 0, which would work, but
          it could be (void*)0. Either use '\0' or 0 in checking for the null
          terminator in a string.
          [color=blue]
          > {
          > if(str[i]==' ')
          > { st[j++]=st[i++];
          >
          > while(str[i]==' ') i++;
          >
          > }
          > else
          > st[j++]=str[i++];
          > }
          > st[j]=NULL;
          > strcpy(str,st);
          > puts(str);
          > }
          >
          > (compiled in turbo c++ for windows version 3.1)
          > i know there should be other ways to write this program (thats a bad
          > program ),can someone help me in writing that program in compact way
          > and give me some logic and tips.
          > i want to use gcc but i don`t have linux operating system and one
          > software which allows gcc compiling in windows (cygwin) is not
          > working. anyone can advice me such a program that allows compiling
          > with gcc in windows( this is off topic but advices are always
          > welcome).[/color]

          Do you want the program that K&R assigned, or the one you tried? It can
          be made much more simple by just immediately outputting characters
          instead of copying to buffers.

          /* algorithm portion */

          int c;
          int flag = 0;

          while ((c = getchar()) != EOF)
          {
          if (c == ' ')
          {
          if (flag == 0)
          {
          putchar(c);
          flag = 1;
          }
          }
          else
          {
          if (flag == 1)
          {
          flag = 0;
          }
          putchar(c);
          }
          }





          Brian

          Comment

          • CBFalconer

            #6
            Re: a little mistake

            ash wrote:[color=blue]
            >[/color]
            .... snip ...[color=blue]
            >
            > i want to use gcc but i don`t have linux operating system and one
            > software which allows gcc compiling in windows (cygwin) is not
            > working. anyone can advice me such a program that allows compiling
            > with gcc in windows( this is off topic but advices are always
            > welcome).[/color]

            djgpp, at <http://www.delorie.com >. However cygwin should work.

            --
            "A man who is right every time is not likely to do very much."
            -- Francis Crick, co-discover of DNA
            "There is nothing more amazing than stupidity in action."
            -- Thomas Matthews

            Comment

            • Julian V. Noble

              #7
              Re: a little mistake

              Malcolm wrote:[color=blue]
              > "ash" <ashishmourya21 @rediffmail.com > wrote in message
              > news:1150482182 .254438.295290@ i40g2000cwc.goo glegroups.com.. .[color=green]
              >> i am writing this program (for exercise1-9 in k&r-2nd edition) which
              >> removes extra spaces in string
              >> example- "test string" will be "test string"
              >>
              >> #include<stdio. h>
              >> #include<string .h>
              >> #include<conio. h>
              >> main()
              >> {
              >> char str[20],st[20];
              >> int i=0,j=0;
              >> printf("enter string");
              >> gets(str);
              >>
              >> while(str[i]!=NULL)
              >> {
              >> if(str[i]==' ')
              >> { st[j++]=st[i++];
              >>
              >> while(str[i]==' ') i++;
              >>
              >> }
              >> else
              >> st[j++]=str[i++];
              >> }
              >> st[j]=NULL;
              >> strcpy(str,st);
              >> puts(str);
              >> }
              >>
              >> (compiled in turbo c++ for windows version 3.1)
              >> i know there should be other ways to write this program (thats a bad
              >> program ),can someone help me in writing that program in compact way
              >> and give me some logic and tips.
              >> i want to use gcc but i don`t have linux operating system and one
              >> software which allows gcc compiling in windows (cygwin) is not working.
              >> anyone can advice me such a program that allows compiling with gcc in
              >> windows( this is off topic but advices are always welcome).
              >>
              >> thankx
              >>[/color]
              > Separate out ewhat you are doing into a function.
              >
              > In this case you want
              >
              > /*
              > turns whitespace spans into single spaces
              > (and trims leading / trailing white space?)
              > */
              > void onespace(char *out, char *in)
              >
              > Decide if you want to allow out and in to be the same. This will make your
              > function
              > more convenient to use, but you might have to be a little bit more careful
              > in coding it.
              >
              > Basically you maintain two pointer, a "write" pointer and a "read" pointer.
              > If you hit
              > a non-whitespace you write to the "write" pointer and increment both.
              > If you hit a whitespace character, you write a single space to the "write"
              > pointer. Then you increment the "read" pointer whilst it is still pointing
              > to white space.[/color]

              I think you also need a little state machine: if you have not hit
              a blank you are in character-accepting mode and write that char
              to the buffer where you construct the compacted string. Otherwise
              if the char is a blank you accept it but shift to a mode where you
              no longer accept blank characters. When you hit a non-blank you
              switch back to the first mode. In other words,

              state\input -> non-blank blank
              // --------------------------------------------------------
              1 write to buff write to buff and
              set state to 2

              2 write to buff and drop char
              set state to 1
              // --------------------------------------------------------

              A switch statement and a state variable will accomplish this.


              --
              Julian V. Noble
              Professor Emeritus of Physics
              University of Virginia

              Comment

              • lovecreatesbeauty

                #8
                Re: a little mistake

                ash wrote:[color=blue]
                > can someone help me in writing that program in compact way
                > and give me some logic and tips.[/color]


                I don't know whether my program is compact or not, anyway code below
                it's my own. Suggestion welcomed.


                /* Removes extra spaces in string pointed by src, the result string is
                pointed by dst. Return value is the length os the result string or
                0 for failure. example: "test string" will be "test string" */
                int spacerm(char *dst, char *src)
                {
                int last_space = 0; /*1: last char is a space char; 0: not*/
                int len = 0;
                int ret = 1; /*for return value, 0: failure, non-zero: result
                string length returned*/

                if (src == 0 || dst == 0)
                {
                ret = 0;
                }

                if (ret != 0)
                {
                while (*src != '\0')
                {
                if (*src == ' ')
                {
                if (last_space != 1)
                {
                *dst++ = *src++;
                len++;
                }
                else
                {
                src++;
                }

                last_space = 1;

                }
                else
                {
                *dst++ = *src++;
                len++;
                last_space = 0;
                }
                }

                ret = len;
                }

                return ret;
                }

                #include <stdio.h>
                #include <string.h>
                int main(void)
                {
                char *s = "test string";
                char a[20] = {'\0'};
                int i = 0;

                printf("%i, %s\n", strlen(s), s);
                i = spacerm(a, s);
                printf("%i, %s\n", i, a);

                return 0;
                }

                $ gcc -W -Wall -pedantic -ansi test.c
                $ ./a.out
                14, test string
                11, test string
                $

                [color=blue]
                > i want to use gcc but i don`t have linux operating system and one
                > software which allows gcc compiling in windows (cygwin) is not working.
                > anyone can advice me such a program that allows compiling with gcc in
                > windows( this is off topic but advices are always welcome).[/color]


                Use mingw or Install a Debian Linux on VMware hosted in Windows.

                lovecreatesbeau ty

                Comment

                • Peter Nilsson

                  #9
                  Re: a little mistake

                  lovecreatesbeau ty wrote:[color=blue]
                  >
                  > /* Removes extra spaces in string pointed by src, the result string is
                  > pointed by dst. Return value is the length os the result string or
                  > 0 for failure. example: "test string" will be "test string" */[/color]

                  What if the original string is "". Is that an error? If so, why?
                  [color=blue]
                  > int spacerm(char *dst, char *src)
                  > {
                  > int last_space = 0; /*1: last char is a space char; 0: not*/
                  > int len = 0;
                  > int ret = 1; /*for return value, 0: failure, non-zero: result
                  > string length returned*/[/color]

                  You effectively have two variables for the same thing. This
                  looks clumsy.
                  [color=blue]
                  > if (src == 0 || dst == 0)[/color]

                  Why bother? [Rehtorical]
                  [color=blue]
                  > {
                  > ret = 0;
                  > }
                  >
                  > if (ret != 0)
                  > {
                  > while (*src != '\0')
                  > {
                  > if (*src == ' ')
                  > {
                  > if (last_space != 1)[/color]

                  if (last_space == 0)

                  Apart from being more efficient on many systems, it's
                  obvious what the test is. When you test explicitly against
                  a value like 1, the reader may pause to ponder whether
                  the variable can other values like 2 or 3 and why 1 is
                  significant.
                  [color=blue]
                  > {
                  > *dst++ = *src++;
                  > len++;
                  > }
                  > else
                  > {
                  > src++;
                  > }
                  >
                  > last_space = 1;
                  >
                  > }
                  > else
                  > {
                  > *dst++ = *src++;
                  > len++;
                  > last_space = 0;
                  > }
                  > }[/color]

                  You never write a terminating null byte to dst.
                  [color=blue]
                  > ret = len;
                  > }
                  >
                  > return ret;
                  > }
                  >
                  > #include <stdio.h>
                  > #include <string.h>
                  > int main(void)
                  > {
                  > char *s = "test string";
                  > char a[20] = {'\0'};[/color]

                  Try...

                  char a[20] = "xxxxxxxxxxxWha t?";
                  [color=blue]
                  > int i = 0;
                  >
                  > printf("%i, %s\n", strlen(s), s);[/color]

                  strlen() returns a size_t value; %i expects an int.

                  In C99 use %zu to print a size_t. In C90, cast the value
                  to suitable unsigned type and use %u with an appropriate
                  length modifier.
                  [color=blue]
                  > i = spacerm(a, s);
                  > printf("%i, %s\n", i, a);
                  >
                  > return 0;
                  > }
                  >
                  > $ gcc -W -Wall -pedantic -ansi test.c[/color]

                  Not a blip eg? Safe as houses then.

                  As some non-expert whose comments aren't worth the
                  em waves they're transmitted in once said "You control
                  the portability of your code, not gcc." <g>
                  [color=blue]
                  > $ ./a.out
                  > 14, test string
                  > 11, test string
                  > $[/color]

                  --
                  Peter

                  Comment

                  • Frederick Gotham

                    #10
                    Re: a little mistake

                    ash posted:
                    [color=blue]
                    > i am writing this program (for exercise1-9 in k&r-2nd edition) which
                    > removes extra spaces in string
                    > example- "test string" will be "test string"[/color]


                    I'd probably approach it something like:

                    #include <cstddef>

                    void RetreatStrXPlac es(register char *p, std::size_t const places)
                    {
                    /* Consider coding this by copying int's
                    and checking if any byte is a space
                    character */

                    if ( !places ) return;

                    register char *q = p - places;

                    while ( *q++ = *p++ );
                    }

                    void FixMultipleSpac es( char * const p_start )
                    {
                    for(char *p = p_start; ; )
                    {
                    switch( *p++ )
                    {
                    case 0: return;

                    case ' ':
                    {
                    unsigned places = 0;

                    while( *p++ == ' ' ) ++places;

                    RetreatStrXPlac es(--p, places);
                    }
                    }
                    }
                    }

                    #include <iostream>
                    #include <cstdlib>

                    int main()
                    {
                    using std::cout;

                    char buffer[] = "The man walked over the revine";

                    FixMultipleSpac es(buffer);

                    cout << buffer << '\n';

                    std::system("PA USE");
                    }





                    --

                    Frederick Gotham

                    Comment

                    • fgothamNO@SPAM.com

                      #11
                      Sorry wrong language

                      Frederick Gotham posted:

                      [color=blue]
                      > #include <iostream>[/color]


                      Sorry wrong language.



                      --

                      Frederick Gotham

                      Comment

                      • Richard Heathfield

                        #12
                        Re: a little mistake

                        Frederick Gotham said:
                        [color=blue]
                        > ash posted:
                        >[color=green]
                        >> i am writing this program (for exercise1-9 in k&r-2nd edition) which
                        >> removes extra spaces in string
                        >> example- "test string" will be "test string"[/color]
                        >
                        >
                        > I'd probably approach it something like:
                        >
                        > #include <cstddef>[/color]

                        No such header.
                        [color=blue]
                        >
                        > void RetreatStrXPlac es(register char *p, std::size_t const places)[/color]

                        Unknown identifier, std
                        Syntax error: "::"

                        Just the first three of many.

                        --
                        Richard Heathfield
                        "Usenet is a strange place" - dmr 29/7/1999

                        email: rjh at above domain (but drop the www, obviously)

                        Comment

                        • Joe Estock

                          #13
                          Re: a little mistake

                          ash wrote:[color=blue]
                          > i am writing this program (for exercise1-9 in k&r-2nd edition) which
                          > removes extra spaces in string
                          > example- "test string" will be "test string"[/color]

                          Sounds simple enough. The portable solution would be to encase this
                          functionality into a function so that you can easily utilize it among
                          other utilities, but for now I'll keep it simple.
                          [color=blue]
                          >
                          > #include<stdio. h>
                          > #include<string .h>
                          > #include<conio. h>[/color]

                          DOS nonsense. This is a non-standard header and looking at your code
                          below you use nothing in it. Get rid of it.
                          [color=blue]
                          > main()[/color]

                          main returns a value. _ALWAYS_.
                          [color=blue]
                          > {
                          > char str[20],st[20];
                          > int i=0,j=0;
                          > printf("enter string");[/color]

                          The user might not ever see this. If you really want to suppress the
                          newline (which ensures that the data gets written to the screen) then
                          consider calling fflush(stdout) immediately after.
                          [color=blue]
                          > gets(str);[/color]

                          Don't use gets(). This function is dangerous. There is a replacement
                          that one of the regulars of this newsgroup posted a while ago that is a
                          much safer alternative. Personaly I would use fgets but anything that
                          accepts a width parameter would be marginally better.
                          [color=blue]
                          >
                          > while(str[i]!=NULL)[/color]

                          NULL is a pointer (in most cases it's 0 cast to a pointer to void, but
                          that's beside the point). While this may work on your system and perhaps
                          many other systems this is a recipe for UB (what happens if NULL is
                          defined to (void *)1?).
                          [color=blue]
                          > {
                          > if(str[i]==' ')
                          > { st[j++]=st[i++];
                          >
                          > while(str[i]==' ') i++;
                          >
                          > }
                          > else
                          > st[j++]=str[i++];[/color]

                          Bad logic. This will not do what you expect it to do.
                          Your string "test string" will become "teststring ".
                          [color=blue]
                          > }
                          > st[j]=NULL;[/color]

                          Again, it's safer to use \0.
                          [color=blue]
                          > strcpy(str,st);
                          > puts(str);[/color]

                          This is a waste of cpu cycles. Just print out st instead and save the
                          overhead of the unneeded function call.
                          [color=blue]
                          > }
                          >
                          > (compiled in turbo c++ for windows version 3.1)
                          > i know there should be other ways to write this program (thats a bad
                          > program ),can someone help me in writing that program in compact way
                          > and give me some logic and tips.
                          > i want to use gcc but i don`t have linux operating system and one
                          > software which allows gcc compiling in windows (cygwin) is not working.
                          > anyone can advice me such a program that allows compiling with gcc in
                          > windows( this is off topic but advices are always welcome).
                          >
                          > thankx
                          >[/color]

                          Here is a modified version of your program that should perform better
                          than the one above. I've tested this however there is always room for
                          human error so be forewarned.

                          #include <stdio.h>
                          #include <string.h>

                          int main(void)
                          {
                          char str[20], st[20];
                          int i = 0, j = 0;
                          size_t len = 0;

                          printf("enter string: ");
                          fflush(stdout);

                          /* note that our string will also have
                          * a newline character at the end of it */
                          fgets(str, 20, stdin);
                          len = strlen(str);

                          /* comment this out if you want to retain the
                          * newline */
                          if (str[len - 1] == '\n')
                          {
                          str[len - 1] = '\0';
                          len--;
                          }

                          for (i = 0; i < len; i++)
                          {
                          /* if the next two characters are spaces then
                          * we need to condense them into one */
                          if (str[i] == ' ' && str[i + 1] == ' ')
                          {
                          st[j++] = ' '; /* we want to keep one space */
                          while (str[i] == ' ')
                          {
                          i++;
                          }
                          }

                          st[j++] = str[i];
                          }

                          st[j] = '\0';

                          printf("%s\n", st);

                          return(0);
                          }

                          Comment

                          • ash

                            #14
                            thankx

                            thnankx joe estock, i really like your code.Its wonderful to see "many
                            logics to solve one question". can u suggest how to develope logics
                            faster?

                            Comment

                            • Joe Estock

                              #15
                              Re: thankx

                              ash wrote:[color=blue]
                              > thnankx joe estock, i really like your code.Its wonderful to see "many
                              > logics to solve one question". can u suggest how to develope logics
                              > faster?
                              >[/color]

                              What code? Without context I have no idea what you are talking about. >
                              Please include enough of the previous post in your reply so that
                              everyone can follow the discussion. It's by chance that I checked this >
                              thread again today. For those of you who do not have access to the
                              previous thread I have included it's context below.
                              [color=blue][color=green]
                              >> ash wrote:
                              >> i am writing this program (for exercise1-9 in k&r-2nd edition) which
                              >> removes extra spaces in string
                              >> example- "test string" will be "test string"[/color][/color]
                              [color=blue]
                              > Sounds simple enough. The portable solution would be to encase this
                              > functionality into a function so that you can easily utilize it among
                              > other utilities, but for now I'll keep it simple.[/color]

                              [snip code from OP]
                              [color=blue][color=green]
                              >> {
                              >> if(str[i]==' ')
                              >> { st[j++]=st[i++];
                              >>
                              >> while(str[i]==' ') i++;
                              >>
                              >> }
                              >> else
                              >> st[j++]=str[i++];[/color][/color]
                              [color=blue]
                              > Bad logic. This will not do what you expect it to do.
                              > Your string "test string" will become "teststring ".[/color]

                              [snip some of my code]
                              [color=blue]
                              > for (i = 0; i < len; i++)
                              > {
                              > /* if the next two characters are spaces then
                              > * we need to condense them into one */
                              > if (str[i] == ' ' && str[i + 1] == ' ')
                              > {
                              > st[j++] = ' '; /* we want to keep one space */
                              > while (str[i] == ' ')
                              > {
                              > i++;
                              > }
                              > }
                              >
                              > st[j++] = str[i];
                              > }[/color]

                              I do not fully understand your question "develop logics faster". As per
                              my original reply I was stating that the logic in your code was
                              incorrect. In this context logic means the "logical series of events and
                              results". You wanted to remove extra spaces (in essence you wanted to
                              condense multiple spaces into one single space). Your original code was
                              removing *all* spaces.

                              The only way I can think of to explain logic is by example. Consider the
                              following line:

                              10,20,30,40\n

                              Let's presume that this line is from a file that contains several other
                              lines like it. We want to read each line in this file and print the
                              information in a more human-readable format. The logic of our program
                              would be something like the following:

                              Open the file for reading.
                              Ensure open was successful.
                              Print the column header.
                              Read a line from the file.
                              While there are lines left in the file...
                              Separate the values by splitting the string on ",".
                              Print out the values.
                              Read another line from the file.
                              End while.
                              Close file.
                              Return status.

                              Comment

                              Working...