/*Preprocessors*/
#include <iostream> //includes basics
#include <string>
#include <cstring>
#include <iomanip>
#include <cstdlib>
//Chris Rachal ITP 232 Lesson 3 assignment
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); i >= 0; i--)
{
cout<<words[i]<<endl;
}
cout<<endl;
}
/**********************************************************************************************************************/
This is such a fun class. I really enjoy learning all there is to know about C++.
Original Order
This is such a fun class. I really enjoy learning all there is to know about C++.
Reverse Order
.
+
+
C
t
u
o
b
a
w
o
n
k
o
t
s
i
e
r
e
h
t
l
l
a
g
n
i
n
r
a
e
l
y
o
j
n
e
y
l
l
a
e
r
I
.
s
s
a
l
c
n
u
f
a
h
c
u
s
s
i
s
i
h
T
Process returned 0 (0x0) execution time : 30.200 s
Press any key to continue.
Asymmetry.
What is the index of the last array element printed by the printOriginal() ?
What is the index of the first array element printed by the printReverse() ?
I'm looking now. Going to take a break and step away from the laptop for a bit. The kids are going crazy. Seriously again, I appreciate the help. At least I got it to print backwards. Just need to change some stuff to and figure out how come the first function isn't working. I don't expect it to be easy right away, I just get frustrated because I just want to be good at it. There is so much to learn and I want to learn all I can.
@Furry Guy, your code that you had earlier makes more sense to me than what I was trying to do. I guess I really need to keep in mind that there can be many ways to solve a problem. Ok, from here I need to try and figure out how to print backwards and count the letters in each word. Also find the min and max letter in the char array. Also the total. lol.
You aren't using a C++ std::string (<string>), you are using C strings. char arrays, and <cstring> to get the C string's length.
From cppreference's page on strlen it has a possible implementation of the function. Using char pointers to find the C string's null ('\0') terminator. You could use the code for your own strlen function. https://en.cppreference.com/w/cpp/string/byte/strlen
1 2 3 4 5 6
std::size_t strlen(constchar* start)
{
constchar* end = start;
while (*end++ != 0);
return end - start - 1;
}
Your instructor wanted you to use C strings and the null terminator. With that code you will.
While I was playing around modifying the C string code earlier to use std::string and iterators I kept wondering why using std::string::iterator wasn't a valid type to use with const std::string iterators.
After a number of minutes it dawned on me what I was doing wrong. const std::string iterators need to be type std::string::const_iterator.
The problem is that the pointer p1 is assigned only once the address s outside the loop. At the first iteration of the loop, p1 points to the first character s. At the second iteration (and at all subsequent ones), p1 does not indicate the beginning of the line, but to where he was pointing at the end of the previous iteration, since the beginning of the array s was not reset.
My instructor gave me this as a sample to try and modify it for me
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
cout << "Your sting backwards is:\n";
int i; //counter var for 'for' loop
int count = 0; //counter var while loop
while (strPtr[count] != '\0') //loop until null encountered
{
if (strPtr[count] == ' ') //loop until whitespace encountered
{
for (i = count - 1; i >= 0 && strPtr[i] != ' '; i--) //decrements through the string
cout << strPtr[i];
cout << " ";
//prints whitespace inbetween words looped through
}
count++; //adds one to string array element each time loop iterates
}
for (i = count - 1; i >= 0 && strPtr[i] != ' '; i--) //decrements through the string
cout << strPtr[i];
cout << endl;
cout << endl;