Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (blackstreet23):

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.

OpenStudy (blackstreet23):

#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; }

OpenStudy (blackstreet23):

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

OpenStudy (blackstreet23):

what does char str[] mean?

OpenStudy (maitre_kaio):

I'm not a C expert, but doesn't it mean that the variable str is an array of char ?

OpenStudy (woodrow73):

^^ that's what it would be if it was in java; idk about C either.

OpenStudy (turingtest):

@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.

OpenStudy (turingtest):

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.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
Mari103: How to pop out like a Jacc In the box
13 hours ago 0 Replies 0 Medals
Breathless: Spooky witch but cute
20 hours ago 3 Replies 0 Medals
Arriyanalol: help
19 hours ago 10 Replies 2 Medals
Arriyanalol: @tinydinoUwU stop trying to find a argument u blad lil boy
1 day ago 5 Replies 4 Medals
Jaded012023: Please tell me what you all think of this song
22 hours ago 6 Replies 1 Medal
Arriyanalol: bro how
22 hours ago 2 Replies 3 Medals
Arriyanalol: cant wait for the new bluey movie in 2027
1 day ago 12 Replies 2 Medals
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!