Can someone explain me how this program works? I have been watching a series of videos to learn c++ and this program was the final project. I was totally lost.
#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; }
It is suppose to print "Hello World" without spaces and print Hello World backwards
what does char str[] mean?
I'm not a C expert, but doesn't it mean that the variable str is an array of char ?
^^ that's what it would be if it was in java; idk about C either.
@maitrekaio yeah, means the same in C++ @blackstreet you should focus on understanding each function in the program individually, then see how they fit together. ``` void reverseChar(char str[]); int countChar(char[]); void removeSpaces(char[]); ``` These three functions are what are used to print "Hello world" in reverse. Each does what it says: ` reverseChar(char str[]) ` takes an array of `char`s (essentially a 'string' in most languages) and reverses it. ` countChar(char str[]) ` counts the number of `char`s in the array `str` and returns the number of elements as an `int`. ` removeSpaces(char str[]) ` takes an array of `char`s and removes the spaces and prints the result to the screen. This program doesn't seem completed. It is still in test form and cannot pass strings between functions, so it can't take dynamic input. It should only print the results of the "test cases" (the hard-coded inputs) to the screen.
If we wanted this program to take inputs it should be evaluating `argc` and `argv` (which should become probably become `str`), and it should be passing `str` between `countChar` and `reverseChar` instead of just printing it to the screen.
Join our real-time social learning platform and learn together with your friends!