MY CODE DOES NOT WORK, AND I REALLY DONT KNOW WHY! I TRIED TO FIGURE OUT EVEN WITH PEN AND PAPER, BUT EVERYTHING SEEMS PERFECT EXCEPT OUTPUT.
I WANTED TO MAKE PROGRAME WHICH DOES THIS
I ENTER 2398, I GET 8932
I ENTER 5467, I GET 7645 AND SIMILAR.
MY CODE:
#include <iostream>
#include <math.h>
using namespace std;
int invbr =0;
int Inv3(int x)
{
int inverzno = (x%10) * 100 + ((x/10)%10) * 10 + x/100;
return inverzno;
}
int Inv(int u)
{
int f;
int p =1;
int j=1;
int brojac =0;
int y =u;
Sorry, dont wanna hijack OP's thread, but I cant seem to understand why your code actually works. If I enter a 3 digit number (like 238), i is initialized to 3 (because there are 3 characters), hence it first outputs word[3]. But how is this possible when the highest element number in a 3 character string is 2? The code seems to suggest that word[3] exists?
Very good question!
NO, word[3] does not exist. However, whenever you initialize an array with certain elements and try to access an element that is out of the array's size limit, the compiler makes that element and sets it equal to nothing.
So in this case, the element at word[3] is equal to "". (It's not space or endline) It is just 'no character'.
In an array of ints, that element would be 0 or 1 depending on the compiler.
That is false. In most cases, trying to access outside of the bounds of the size of a string with operator[] will result in an assertion failure. However, it's undefined behavior even if it does work.
NO, word[3] does not exist. However, whenever you initialize an array with certain elements and try to access an element that is out of the array's size limit, the compiler makes that element and sets it equal to nothing.
So in this case, the element at word[3] is equal to "". (It's not space or endline) It is just 'no character'.
In an array of ints, that element would be 0 or 1 depending on the compiler.
If it is an array of characters (string) then the last element would be a null terminator ('\0') otherwise it is undefined not 0 or 1. Either way it is not the best practice.
As far as this problem the easiest solution would be something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>
int main()
{
std::string str = "";
std::cout << "Please enter a string to be reversed: ";
std::getline(std::cin, str);
std::cout << "Before: " << str << '\n'
<< "After: " << std::string(str.rbegin(), str.rend()) << std::endl;
return 0;
}
Please enter a string to be reversed: 2398
Before: 2398
After: 8932
The reason this works is because of the range based constructor and reverse iterators.
#include <iostream>
int main()
{
int n = 0;
std::cout << "Please enter a number to be reversed: ";
std::cin >> n;
std::cout << "Before: " << n << '\n'
<< "After: ";
while(n) //while n has a value still
{
std::cout << n % 10; //grab right hand digit
n /= 10; //remove right hand digit
}
std::cout << std::endl;
return 0;
}
Please enter a number to be reversed: 2398
Before: 2398
After: 8932
EDIT: Accidentally forgot the second parameter (funnily enough I had it in the shell I was running but some how it pasted incorrectly, might have pasted old clipboard)
@gilbit: Can you briefly explain what the functions rbegin() and rend() do to reverse a string? What exactly is going on 'under the hood' when you use these two functions?
rbegin returns an Iterator type with operator++ overloaded. It returns an iterator to the end of the iterable container, and moves down on each increment.