i realize the topic isnt much descriptive but i thought that my code was a bit strange so i hope dw can give me a bit of info this is the most random thing ive ever made.
on windows it produces quite a bit of ruckus in the console but if i put the loop going forward no error ???? confused me too.

void backwards(string input){
    int loop,o;
    string i;
    i=input;
    string ot;

    for(loop=sizeof(i)+1;loop!='\n';loop--){
        cout<<i[loop];
    }

//    for(loop=0; loop!='\n';loop++){ this is the forward loop or i at least i believe its opposite is the above code.
//        cout<<i[loop];
//    }
}

im kinda curious as to why it does what it does.
memory leak ?

Recommended Answers

All 19 Replies

When you apply sizeof to an object of type string , you get the amount of memory that needs to be allocated for an object of that type, exclusive of dynamic memory. This number has nothing to do with the number of characters in the string , so the results you get will have little if any connection to anything useful.

That, and you should loop till loop = 0, not till "\n", because it will take it as the ASCII value of \n (which is 10 I guess). And to tackle the problem of getting the string size, you could get the string class to return the character array of the string, and then use strlen to find the length of the string

And to tackle the problem of getting the string size, you could get the string class to return the character array of the string, and then use strlen to find the length of the string

Or use the .length() member of your string.

thanks guys i fixed it just had to remember what i did last time :)
didnt know that about sizeof() though.
anyway here is the final product although i was wondering i was trying earlier to store the result in another string(with the function having a return type) but i couldnt quite get it.
if i were to create a function that returns a string would i use char backwards(string input,string output){
//blah blah blah
return output;
} ?
because my understanding is that a string is just a preset char array ?

//Project Header:Ava.h

void backwards(string input){
    int loop,o;
    string i;
    i=input;
    string ot;
    for(loop=i.length();loop!=0;loop--){
        cout<<i[loop-1];
    }
/*The Below Loop is the forward version*/
//    for(loop=0; loop!=i.length();loop++){
//        cout<<i[loop];
//    }
}
//And here it is in use

#include <iostream>
#include "ava.h"
using namespace std;

int main()
{
    int loop;
    loop=0;
    for(loop=0;loop<1000;loop++){
   string input,q;
    q="Input";
    query(q);
    cin>>input;
    cout<<"Input backwards is :";
    backwards(input);
    cout<<'\n';
    cin.get();
    }
    return 0;
}

i would really like to have the function return a string instead of a void function that does all the work.

I would re write your function like this

string backwards(string input)
{
    string temp;
    for (size_t i = input.size() - 1; i >= 0; i--)
       temp += input[i];
    return temp;
}

// then in you main function 
cout<<"Input backwards is : " << backwards(input);

FYI size_t is the type returned by the size() function. It is normally an unsigned int.

I would re write your function like this

string backwards(string input)
{
    string temp;
    for (size_t i = input.size() - 1; i >= 0; i--)
       temp += input[i];
    return temp;
}

// then in you main function 
cout<<"Input backwards is : " << backwards(input);

FYI size_t is the type returned by the size() function. It is normally an unsigned int.

thanks but uhm when it compiles it runs then windows generates an error what compiler were you using im using gcc 3.4.5 (getting ready to update though to 5.16)
also im not an expert but i thought you couldn't declare a string function as string is essentially a char array ?? can anybody shed some light on this ?

EDIT: apparently you can XD. Nathan i fixed it to compile for me anyways
i couldnt get any combination of size_t i = input.size() to work
whats the difference between size() and length()
i would assume size is the amount of memory taken up by said string and length is how long it is

string backwards(string &input)
{
    string temp;
    for (int i = input.length() - 1; i >= 0; i--)
       temp += input[i];
    return temp;
}

What error are you getting? I am using MSVC++ 2005

You should get an error with that code [Nathan's] since size_t is normally unsigned.
That means is it is always >0. Use a long int and a cast. Alternatively use an iterator.

You can also offset by one

for(size_t i=input.size();i;i--)
   temp+=input[i-1];

But I prefer iterators.

Sorry I found what it was. Since we are going backwards I forgot size_t wont work. If you change the for loop to this it should work.

for (int i = input.size() - 1; i >= 0; i--)

i fixed it :)

the edit is above but since alot of people skim (myself included :)) ill post down here
by the way what is the difference between size() and length() because both solutions work (ie .length() and .size() both work)?

string backwards(string &input)
{
    string temp;
    for (int i = input.length() - 1; i >= 0; i--)
       temp += input[i];
    return temp;
}

Considering Nathans code: using int i.

Please don't use an int like this, input.size() is normally long int. You (a) should get a nasty warning. (b) it is an input dependent bug.. they are the worst kind so please code defensively for it.

what is the difference between size() and length() because both solutions work (ie .length() and .size() both work)?

@ stuXYZ According to my MSDN a long int and a int are the same thing? are the different with the compiler you are using? I re wrote the for loop this way to make it work with size_t and not have to use an offset.

for (size_t i = input.size() - 1; i != -1; i--)

@ stuXYZ According to my MSDN a long int and a int are the same thing? are the different with the compiler you are using? I re wrote the for loop this way to make it work with size_t and not have to use an offset.

for (size_t i = input.size() - 1; i != -1; i--)

Long ints
and int are slightly different
in that a long int can have a higher value than a normal int :)

Yes the minimum is 4 but what do you get for output if you run this

#include <iostream>
using namespace std;


int main()
{
    cout << sizeof(long int);
    cin.get();
    return 0;
}

I got 4 for my output.

Yes the minimum is 4 but what do you get for output if you run this

#include <iostream>
using namespace std;


int main()
{
    cout << sizeof(long int);
    cin.get();
    return 0;
}

I got 4 for my output.

i got 4 as well :)
EDIT: i made a tard comment sorry.:0 the actual difference is the memory consumption
Thanks to geo121 from wrox for this info:

Well the difference is very small but can be very important in some instances. When programming the second most important objective (after making the program do what it is supposed to) is saving memory to make the program run as fast as possible. This is where your keyword "long" comes in. long is an integer but integer isn't exactly a long understand? There is also a "short". now usually these are referenced with int after them but it is not required. The main difference between them is their size. Depending on the computer but in most cases a "short" is two bytes a "long" is four bytes and an "int" can be two or four bytes. It is important in programs that you declare it short or long if you know exactly how big the number is going to be and how often it is going to modified so that you can save as much memory as possible. Now as far as the range that depends on the size of the int whether it is two or four bytes. Here is a table to reference sizes and values.

type size range
short int 2 bytes -32,768 to 32,767
long int 4 bytes -2,147,483,648 to 2,147,483,647
int (16 bit) 2 bytes -32,768 to 32,767
int (32 bit) 4 bytes -2,147,483,648 to 2,147,483,647

There are also signed and unsigned if you need help with those just ask but they are really simple

~ Geo

About the string being a character array, just that the string class, tries to hide (almost) the fact that it is a character array. You could use a character though. A more efficient way of doing things would be to declare a character array of the size of the string and then return it.

About the string being a character array, just that the string class, tries to hide (almost) the fact that it is a character array. You could use a character though. A more efficient way of doing things would be to declare a character array of the size of the string and then return it.

Thanks for clearing that up i was pretty sure that it was a char array but didnt know for certain ;)

Thanks Everyone \|/

About the string being a character array, just that the string class, tries to hide (almost) the fact that it is a character array. You could use a character though. A more efficient way of doing things would be to declare a character array of the size of the string and then return it.

Thanks for clearing that up i was pretty sure that it was a char array but didnt know for certain ;)

Thanks Everyone :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.