char array and pointer

Pages: 123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*Preprocessors*/
#include <iostream> //includes basics
#include <string>
#include <cstring>
#include <iomanip>
#include <cstdlib>
//Chris Rachal ITP 232 Lesson 3 assignment

using namespace 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;

}
/**********************************************************************************************************************/




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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() ?


What are the purposes of wordsptr?
Last edited on
I will be honest with thats another thing I'm lost at. I'm supposed to make a pointer.
I already showed how to use raw pointers for iterating through a C string.
http://www.cplusplus.com/forum/general/269410/2/#msg1159520

Not the most elegant code, but it works.

The logic in the for loops was based on the idea of C++ container iterators. Half-arsed to use raw char pointers.

(Did I mention you should remember the word interator?)
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.
Last edited on
Phew, back to get guys. Hopefully I can keep the kids at bay for a bit, because this program is due monday and then I start homework.
@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.
Oh man, I just realized I can't use C++ string.
I just realized I can't use C++ string

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(const char* start)
{
   const char* 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.
your code that you had earlier makes more sense to me than what I was trying to do.

The result of experience. Both writing my own code and looking at how others, who are more knowledgeable than I am, assemble their code.

Trial and error and learning from mistakes helps also. Lots and lots of mistakes.
Last edited on
Speaking of mistakes, really dumb ones.....

http://www.cplusplus.com/forum/general/269410/2/#msg1159520

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.

Well, DUH! :)

Or use auto so the compiler can deduce the type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

void forward_print(const std::string&);
void reverse_print(const std::string&);

int main()
{
   std::string str { "This is a test" };

   forward_print(str);

   reverse_print(str);
}

void forward_print(const std::string& str)
{
   for (std::string::const_iterator itr = str.cbegin(); itr != str.cend(); itr++)
   {
      std::cout << *itr << ' ';
   }
   std::cout << '\n';
}

void reverse_print(const std::string& str)
{
   for (auto itr = str.crbegin(); itr != str.crend(); itr++)
   {
      std::cout << *itr << ' ';
   }
   std::cout << '\n';
}


This is why I kept babbling on about iterators. Just thought ya should know.

If you don't understand the code ATM, don't sweat it.
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.
Thanks for your help. I am screwed I think lol. I I'm working on the backwards function: olleh

I got the reverse one, but I'm just lost. I don't think i will be able to complete it by tonight
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;
Indentation helps to see scopes. Braces, {}, around the only statement within loop's body do clarify scope too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout << "Your sting backwards is:\n";
int i;
int count = 0;
while ( strPtr[count] != '\0' ) {
    if ( strPtr[count] == ' ' ) {
        for ( i = count - 1; i >= 0 && strPtr[i] != ' '; i-- ) {
            cout << strPtr[i];
        }
        cout << " ";
    }
    count++;
}
for ( i = count - 1; i >= 0 && strPtr[i] != ' '; i-- ) {
    cout << strPtr[i];
}
cout << endl;
cout << endl;

Can you explain the logic of the code?
Topic archived. No new replies allowed.
Pages: 123