reversing char array

Feb 15, 2016 at 3:42am
so im suppose to open a file, create a char array and use getline to read lines from the file and put it in the array. Then im suppose to create a function that will count the character in a char array and shouldnt include the null character, return in to the main function , create a third function to reverse the lines.
i messed up by testing out only one line of words on my file instead of multiple lines and now it doesnt count the characters correctly. can someone help me. ill gladly specify more if anyone is still confused. thanks
file:
how are you
doing today?
i am doing
pretty good
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
#include <iostream>
#include <fstream>

using namespace std;
char charCounter(char array[], const int);
void reverseChar(char array[], int);


int main()
{
   int x = 0;
   const int VAL = 100;
   char array[VAL];
   ifstream file;
   file.open("datEx.txt");


   if (!file)
   {
      cout << "File did not open." << endl;
      system("pause");
      return -1;
   }

   int numberofchar;
   while (file.getline(array, VAL))
   {
       numberofchar=charCounter(array,VAL);
      
     
   } // end of while loop
   cout << " " << endl;
   cout << numberofchar << endl;

   reverseChar(array, numberofchar);
   cout << " " << endl;
   



   file.close();
   system("pause");
   return 0;
}

char charCounter(char array[], const int)
{
   int x = 0;
   
   while (array[x] != '\0')
   {

      cout << array[x];
      x++;
      

   }

   return x;
}

void reverseChar(char array[], int numberofchar)
{
   int u;
   
      for (u = numberofchar - 1; u >= 0; u--)
      {
         cout << array[u];
      }
   

}
Last edited on Feb 15, 2016 at 3:42am
Feb 15, 2016 at 12:40pm
why dont u use strlen and strrev? o.0 0.o
Feb 15, 2016 at 1:56pm
You are printing data immediately in a function so you can only do something like:
1
2
3
4
5
6
7
8
int numberofchar;
while (file.getline(array, VAL))
{
   numberofchar=charCounter(array,VAL);
   cout << "    ";
   reverseChar(array, numberofchar);
   cout << endl;    
}


or like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while (file.getline(array, VAL))
{
   numberofchar=charCounter(array,VAL);
   cout << endl;
}

cout << endl;
file.close();
file.open("datEx.txt");

while (file.getline(array, VAL))
{
    reverseChar(array, numberofchar);
    cout << endl;
}
Topic archived. No new replies allowed.