The name of an array by itself is a pointer to the first element of the array. Passing an array into a function the array decays to a pointer pointing to the first element of the array.
Normally when passing an array into a function the array's size needs to be passed as well. Not needed with C strings since the array is null terminated ('\0').
#include <iostream>
#include <cstring>
void forward_print(char[]);
void reverse_print(char[]);
int main()
{
char cstr[] { "This is a test" };
forward_print(cstr);
reverse_print(cstr);
}
void forward_print(char cstr[])
{
// get the C string's length
size_t len { strlen(cstr) };
// create a pointer to the beginning of the C string
char* begin { cstr };
// create a pointer to one address past the last valid character of the C string
// the address of the null terminator '\0'
char* end { cstr + len };
// create a for loop using pointer math
for (char* itr { begin }; itr != end; itr++)
{
std::cout << *itr << ' ';
}
std::cout << '\n';
}
void reverse_print(char cstr[])
{
// get the C string's length
size_t len { strlen(cstr) };
// create a pointer to the last valid character in the C string
char* rbegin { cstr + len - 1 };
// create a pointer to the address one element before the first valid character
char* rend { cstr - 1 };
// for loop using pointer math
for (char* itr { rbegin }; itr != rend; itr--)
{
std::cout << *itr << ' ';
}
std::cout << '\n';
}
C arrays and pointers/pointer math is a simple form of what C++ calls iterators.
Remember that word, iterator. When you want to loop through the elements of a C++ container, you should use iterators.
I'm still working on that reverse function. If I can eventually be efficient with coding, I would love to pay it forward and help people. I'm going to keep at it. I really put in a lot of time into it and I hope it all pays off.
usingnamespace std;
//Function Prototypes
void getText(char words[]);
void printOriginal(char words[]);
void printReverse(char words[]);
int main()
{
/**************************************************************************************
*This is such a fun class. I really enjoy learning all there is to know about
*C++. Array stores this text. The program must print text in original, reverse,
*backwards order as well as count the letters in each word. The program must
*be able to: count the number of words, longest word length, and shortest word length
***************************************************************************************/
char words[256];//Array to hold text
getText(words);//Calls the function to store data into the array.
printOriginal(words);
printReverse(words);
return 0;
}
//function definitions
//Function definition to have the user type text and store it into the array.
void getText(char words[])
{
cin.getline(words, 256);//saves the data entered by the user.
}
/***************************************************************************************/
//Function displays what the user is prompt to enter.
void printOriginal(char words[])
{
char *wordsptr = words;
cout<<"Original Order"<<endl;
for(int i = 0; i < strlen(words); i++)
{
cout<<words[i];
}
cout<<endl;
}
/***************************************************************************************************************/
void printReverse(char words[])//Function displays text in reverse.
{
char *wordsptr = words;
cout<<"Reverse Order"<<endl;
for(int i = strlen(words)-1; i >= strlen(words); i--)
{
cout<<words[i]<<endl;
}
cout<<endl;
}
/**********************************************************************************************************************/
Just focus on this: If the input word is "hello", then what values does i need to be, in order, to print the letters reversed.
What should the first value of i be.
What should the last value of i be.