Trentacle 112 Junior Poster in Training

Postfix operators bind more tightly than prefix operators. Therefore your *length++ gets parsed as *(length++) , which has undefined behavior because length + 1 isn't a pointer to the same object.

Trentacle 112 Junior Poster in Training

On the same note, requiring less extra space than myk45's suggestion and only somewhat more computationally intensive than mine, keep an array of pointers. Step through the string, and, every time you pass a space, overwrite it with a 0 and store a pointer to the next character in the array.

When you're done, you'll have an array of pointers to null-terminated strings.

temp = array[m-1];
array[m-1] = array[n-1];
array[n-1] = temp;

and loop through the array, printing each one and spaces between each.

Trentacle 112 Junior Poster in Training

Here's what my first-guess algorithm looks like:

1. Start copying characters from name to newName, keeping track of the number of spaces you encounter.
2. When you encounter the (m-1)th space, copy the remaining characters up through the (n-1)th space into a temporary buffer.
3. Copy the nth word from name into newName.
4. Find the first space in the temporary buffer and copy all the characters from that one on into newName.
5. Copy the mth word from the start of the temporary buffer into newName.
6. Copy the remaining characters from name into newName.

There's a (I think) better algorithm, but it involves trampling on the last half of newName instead of using a temporary buffer... I can't work out the details in my head and it would probably just be confusing anyway.

Trentacle 112 Junior Poster in Training

Thanks I will give it a look.

[...]

That is why I'm learning Perl

Good choice, on both counts

p.s. I also recommend K&R, but I'm not sure what other books would be advisable. Most of my knowledge comes from lurking in comp.lang.c

Trentacle 112 Junior Poster in Training

Well, since you posted in the C forum, here's a C example of getting a filename from the user. It performs careful error checking, which may or may not be necessary for your purposes (but you're well advised to always do it).

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

int main(void) {
        char buf[80];
        size_t len;
        printf("Enter a filename: ");
        fflush(stdout);

        if (!fgets(buf, sizeof buf, stdin)) {
                fprintf(stderr, "EOF or input error\n");
                return -1;
        }

        len = strlen(buf);
        if (len + 1 < sizeof buf) { /* end of line reached */
                buf[len - 1] = '\0'; /* remove terminal \n */
        } else {
                fprintf(stderr, "Filename too long!\n");
                return -1;
        }

        printf("You entered '%s'\n", buf);
        return 0;
}

C++ would look quite different, with lots of std::cout and std::cin, I imagine.

Best of luck.

(Man page for fgets is available at http://www.opengroup.org/onlinepubs/009695399/functions/fgets.html . You may see scanf used to read interactive input, but don't do that.)

Trentacle 112 Junior Poster in Training

I don't know where to start.

Are you using C or C++? You can't use <iostream> in C, but "delete" is a reserved word in C++. Neither language has <conio.h> (which is an archaic and non-standard header) or void main().

Your input problems are something else altogether, but you need to decide what language you're using first.

Trentacle 112 Junior Poster in Training

dear all

i am new to perl. when i run my basic perl program as follows

print<<EOF;

HELLO MY NAME IS MANISH ARORA
EOF

i am getting this error

Can't find string terminator "EOF" anywhere before EOF at PERL.plx line 1.

please help

Sounds like there's a stray space after the EOF. Careful with that.

(BTW, there's no need to yell. Perl understands lowercase letters quite as well as caps.)