Switching first and last words of entered string.

the only issue im experiencing is that the last letter of the first word disappears whenever the program processes the string. can you guys possibly help me review my code? i dont know what to do at this point.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <string>
#include "stdafx.h"






void main()
{
char a[30],b[30];
int j=1,k=1,l=1;
int m,n=0;
gets(a);
l=strlen(a);
for(;a[l-k]!=' ';k++)
{}

for(j=0;a[j]!=' ';j++)
{}
j++;
for (m=0;m<j;m++)
b[m]=a[l-k+m];
b[m]=' ';
for(;m<(l-k);m++,j++)
b[m]=a[j];
b[m++]=' ';
for(;m<l;m++,n++)
b[m]=a[n];
b[m]=0;
puts(b);

getch();

}
Last edited on
can anyone help?
You've included <string>, but you aren't using any strings. You're manipulating character arrays instead.

Also, you're using gets(), which you should never ever do. Because you used it, I can supply specially crafted input to your program that will trick it to do whatever I want - I can make it format your hard drive or email me your passwords, all I need is access to your keyboard.

Also, "void main()" is an error and won't compile on most compilers. Use "int main()".

Ignoring error handling and sticking to your logic (scan for space from start, scan for space from end, copy to new stirng), your program could be rewritten this way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <iostream>

using namespace std;
int main()
{
    string a;
    getline(cin, a);

    size_t first_start = 0;
    size_t first_len = a.find(' ');

    size_t last_start = a.rfind(' ') + 1;
    size_t last_len = a.size() - last_start;

    size_t mid_start = first_len;
    size_t mid_len = last_start - first_len;

    string b = a.substr(last_start, last_len)
             + a.substr(mid_start, mid_len)
             + a.substr(first_start, first_len);

    std::cout << b << '\n';
}


online demo: http://ideone.com/zWeWj
is it possible for you to include pointers in that?
why use pointers?
i have to compile this on a c compiler. it wont work otherwise.. :|
Last edited on
i tried using character arrays in place of the strings and trying some isspace functionality but the code was so messy, that i just couldnt get it to work.
I am looking at the code, what is suppose to do? In other what is the problem you trying to accomplish? This is a c++ forum but some of us know how to do things in c, without c++, which makes the void main() perfectly legal.

if I am breaking the a string into words, in c, the code might look something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include <stdio.h>

int main()
{
     char a[30],b[30];  // are these long enough to support the string you want.
     int j=1,k=1,l=1;
     int m,n=0;
     gets(a);
     l=strlen(a);

     char *pA = &a;
     char *pB = &b;

     while(pA)
     {
           if(*pA == ' ') // assuming that is a word break.
           {
                // print the word
                printf("A Word: %s", pB);
                // clear b
                for(j = 0; j < 30; j++) b[j] = 0;
                // reset the pointer.
                pB = &b[0];
           }
           else
           {
                *pB = *pA;
                pB++;
           }
           pA++;
      }
       return 0;
}// end of main. 
Last edited on
FYI, void main is not legal C either.
That has been changed in the standard c too. 20 years ago void main was legal in both languages...
there's a fatal exception there and i dont know where it is....says its a dll error. please help.
Last edited on
@firedraco - Technically, void main is legal C.

C99 wrote:
5.1.2.2.1 Program startup
1 The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):

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

or equivalent;) or in some other implementation-defined manner.


So, while it's legal on any implementation which supports it, no strictly conforming program will use it, and it's use should be avoided at any rate for portability reasons.

@cocopuff - When you encounter an error message, please specify the full text of the message.


Unhandled exception at 0x58ace42e (msvcr100d.dll) in Job2.exe: 0xC0000005: Access violation writing location 0xffffffcc.

i appreciate the help.
If you were looking at Azagaros code, there are a couple things wrong with it.

Line 15 should be while (*pA);

And line 19-20 should be changed to:

19
20
     *pB = '\0' ;
     printf("%s\n", b) ;


It won't, of course, print the last word.
How about this for a C translation of Cubbi's code? (tweaked to handle one word case, too)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <string.h>

int main()
{
    size_t first_start;
    size_t first_len;
    size_t last_start;
    size_t last_len;
    size_t mid_start;
    size_t mid_len;

    char* p;

    char a[30];
    char b[30];

    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));

    gets(a);

    p = strchr(a, ' ');

    if (p != NULL)
    {
        first_start = 0;
        first_len = p - a;

        p = strrchr(a, ' ');
        /*
         * no need to check p here as we found a space already
         * when searching in the other direction.
         */

        last_start = p - a + 1;
        last_len = strlen(a) - last_start;

        mid_start = first_len;
        mid_len = last_start - first_len;

        /*
         * this requires the buffer b to be zeroed
         */
        strncat(b, &a[last_start ], last_len );
        strncat(b, &a[mid_start  ], mid_len  );
        strncat(b, &a[first_start], first_len);
    }
    else
    {
        strcpy(b, a);
    }

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

    return 0;
}
thank you!
Last edited on
Topic archived. No new replies allowed.