Can someone explain me how this program works?

Write your question here.
I have been watching a series of videos to learn c++ and this program was the final project. I was totally lost.

It is suppose to print "Hello World" without spaces and print Hello World backwards

what does char str[] mean?

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
#include <cstdlib>
#include <iostream>

using namespace std;

//Function Declarations
void reverseChar(char str[]);
int countChar(char[]);
void removeSpaces(char[]);

int main(int argc, char *argv[])
{
   //Tests
   removeSpaces("H e  l lo _wor ld");
   cout << endl;
   reverseChar("Hello world");
   
   //Output should be 
   /*Hello_world
   dlrow olleH*/

    char w;
    cin >> w;
    return 0;
}

//Function implementations
int countChar(char str[])
{
   int count=0; //Variable to count the number of characters in the array. Do not use sizeof to get this value.
   char currentChar = str[count];
   
   while(currentChar!='\0')
   {
      count++;
                              
   }
   return count;   
}

void removeSpaces(char str[])
{
   int charCount = countChar(str)+1; //+1 to accommodate null termination
   //--Array that will be result----//
      char resultArray[charCount];
   //---------------------------------//
   for(int i = 0, j=0; j < charCount; j++)
   {
       if(str[j] != ' ')
       {
          resultArray[i] = str[j];
          i++; //Character added to  result. Increase index counter for result.
       }
       else
       {
           //Nothing to do. Check the next character. j will be incremented by for loop.  
       }       
                 
   }
   cout << resultArray;
}

void reverseChar(char str[])
{
   int charCount = countChar(str)+1; //+1 to accommodate null termination
   //--Result Array that will be reversed----//
      char reverseArray[charCount];
   //----------------------------------------//
   for(int i = charCount-2, j=0; i >= 0; i--,j++)
   {
       reverseArray[j] = str[i];  
   }
   cout << reverseArray;
}
This code won't even compile. This line is illegal in c++.
1
2
3
   int charCount = countChar(str)+1; //+1 to accommodate null termination
   //--Array that will be result----//
      char resultArray[charCount]; // <<----illegal 



You would have to use a dynamic array to make this leagal.
Last edited on
I dont really understand much of this code. None of this was actually explained in any of his videos. Could you walk me through the functions please.
char str[] says that the parameter str is an array of characters, but the size is unspecified. The parameter could also be specified as char *str for the same effect, but using [] is a nice hint to the programmer to say "this points to an array of items, not just one.'

Function charCount() doesn't actually work because the loop at lines 33-37 doesn't change currentChar. You may be thinking "but wait! It updates count, and since currentChar is str[count], shouldn't that work?" The answer is no because line 31 assigns the value of str[count] to currentChar, not the formula. When count changes, currentChar stays the same. CharCount should be something like this:
1
2
3
4
5
6
7
int CharCount(const char*str) {
    int count=0;
    while (*str++) {
        ++count;
    }
    return count;
}


removeSpaces() and reverseChar() define an array whose length is determined at runtime. This is non-standard C++. You don't actually need these arrays at all since you're outputting the value. Just output as you go. This has the added advantage of meaning that you don't need to call countChars() at all.

Since the else clause from lines 54-57 is empty, you don't need it, although I know some people who put an else with every if, just to force them to think about the else case.

Taken together, removeSpaces would look like this:
1
2
3
4
5
6
7
8
void removeSpaces(const char str*)
{
    for (; *str; ++str) {
        if (*str != ' ') {
            cout << *str;
        }
    }
}


reverseChar could also be done without copying the array. I'll leave that one to you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void reverseChar (const char *str)
{
    int count=0;
    do
    {
    ++str;
    count++;
    }
    while(*str !='\0');

    do
    {
    --str;
    }while(*str !='\0');

    for(int n=count; n>-1; --n)
        {
        cout << *(str+n);
        }
}

I know how swap using two arrays.
This is how i would do it with pointers.
I assuming that i'm doing it the long and weird way.
Is there an easier way to do this? using pointers?
Is there an easier way to do this? using pointers?

These are untested.
Method 1:
1
2
3
4
5
void reverseChar(const char *str)
{
for (int n=strlen(str); --n >= 0;) {
    cout << str[n];
}

method 2:
1
2
3
4
5
6
7
void reverseChar(const char *str)
{
    if (*str) {
        reverseChar(str+1);
        cout << *str;
    }
}

Neither of these is perfect because they will both fail on very large strings. Method 1 fails if strlen(str) overflows an int. Method 2 will fail when it overruns the stack size. Method will probably fail before method 1.
Topic archived. No new replies allowed.