tags:

views:

124

answers:

3

I have written the code which is too lengthy:

main()
{
    char strEnter[100];
    char strGet[100];

    cout<<"Enter any string:";
    gets(strEnter);

    int counter=0;
    int length=strlen(steEnter)-1;

    while(length>=0)
    {
        if(strEnter[length]==' ')
        {
            for(int i=length+1;strEnter!=null;i++)
            {
                strGet[counter++]=strEnter[i];      
            }

            strGet[counter++]=' ';
            strEnter[length]='\0'; 
        }
        length--;      
    }

    int length1=strlen(strEnter)-1;

    for(int j=0;strEnter!=null;j++)
    {
        strGet[counter++]=strEnter[j];
    }

    strGet[counter]='\0';
    puts(strGet);
}
+2  A: 

You can do it STL algorithms and boost:

    using namespace std;
    using namespace boost::lambda;
    using namespace boost;

string s = "uk based project";

//Split the strings
vector<string> strings;
stringstream ss(s);
copy(istream_iterator<string>(ss), 
        istream_iterator<string>(),
        back_inserter(strings));

//Copy the result in the reverse order
string res;
for_each(strings.rbegin(), strings.rend(), var(res)= var(res) + " " + _1);
Asha
Impressive, but I don't think this is what he wants: since he can't use string functions, I suppose that it's homework, and I don't think they'll let him use boost and STL :D. By the way, it seems that he's also using Turbo C++, does anyone know if boost supports it? :)
Matteo Italia
+2  A: 

Do not use gets(), because it does not check for buffer overrun. Even its manpage tells you to never use it:

SECURITY CONSIDERATIONS

The gets() function cannot be used securely. Because of its lack of bounds checking, and the inability for the calling program to reliably determine the length of the next incoming line, the use of this function enables malicious users to arbitrarily change a running program's functionality through a buffer overflow attack. It is strongly suggested that the fgets() function be used in all cases.

The gets function simply does not know how long your strEnter array is, so it does not know how many characters is safe to put in it.

Messa
could you please tell me how buffer over run happens with gets()
Guri
ok, I have updated the answer
Messa
Thanks you Messa
Guri
+1  A: 

Get the code working before you focus on efficiency.

If you are after efficiency then use pointer arithmetic not indexes:

   char strGet[100];
   char* nextGet = &strGet[0];

and later

   *nextGet++ = strEnter[i];  // use ideally char* on RHS too

You are limiting this to input string length <= 99. Instead use std::string to capture the input.

std::string strEnter;
cin >> strEnter;

This line does not work

for(int j=0;strEnter!=null;j++)

Since strEnter is an array, strEnter is the same as &strEnter[0] - your loop termination condition will always be false and you will loop until you crash. I think you mean:

for(int j=0;strEnter[j]!=0;j++)

Even then, you need to explicitly null-terminate strEnter in input for this to work. Or use std::string which handles this for you.

Once you have the basic version working, it might be a good exercise to recode to reverse word order in place (i.e. only one full-sized buffer can be used). Sometimes memory efficiency is as important as minimizing runtime.

Even though you are not using the runtime string functions (apart from strlen I guess), think about this in terms of clear subtasks and generate appropriate functions of your own to make the design clearer.

read input
while (there is another token in input)
  move token to end of current result
output result
Steve Townsend
for(int j=0;strEnter[j]!=0;j++)
Guri
i here want to check whether the loop is terminating or not.I would use for(int j=0;strEnter[j]!=null;j++)
Guri
I don't use `NULL` or `null` - are you sure `null` (lowercase) will work? This is not C#.
Steve Townsend
for(int j=0;strEnter[j]!=0;j++) is suggested by you.I am sure this will not work in my application
Guri