My assignment is as follows...

Create a program called part4.cpp containing a recursive function that accepts a pointer to a null-terminated string as its argument, and prints the string in reverse order.
Demonstrate the function by calling it from the main function with a string that has been provided by the user. In other words, you will prompt the user to enter a string and the program will disply the string backward.

I'm a little confused as to what a null terminated string really is. Can someone who understands clarify for me what exactly I need to do in this program?

Thanks

Dani AI

Generated

and gave useful starting points. One important clarification to avoid confusion: there are two different “null” things people mix up — a null pointer (pointer == nullptr) and the string terminator (the zero byte stored at the end of a C-style string). For this assignment the recursion should stop when you reach the terminator (the character the pointer points to), and it’s reasonable to add a defensive check for a null pointer if you expect it might ever be passed.

A small, robust approach that handles full-line input (including spaces) and keeps lifetime issues simple is to read into a std::string, then call a recursive routine that accepts a const char *. Example:

#include <iostream>
#include <string>

void printReverse(const char *s) {
    if (!s || *s == '\0') return;   // defensive: nullptr OR end-of-string
    printReverse(s + 1);
    std::cout << *s;
}

int main() {
    std::string input;
    std::getline(std::cin, input);
    printReverse(input.c_str());
    std::cout << '\n';
}

Troubleshooting tips: use std::getline so spaces are preserved; prefer const char * for safety if you do not modify the buffer; remember recursion depth equals string length — for extremely long inputs an iterative method or using reverse iterators on std::string is safer. Finally, if the assignment insists on a raw char[] for input, ensure the array is large enough and null-terminated before calling the recursive function.

Recommended Answers

All 2 Replies

First off, in C, there is no built-in string data type. Therefore, string is represented in an array of characters. The null terminator depicted by a \0 (backslash zero). It is the way of telling where the string ends.

Recursion means a function keeps calling itself until the termination case occurs. You can read more about this if you are not sure what recursion is all about.

Here's the logic:
Implement a function that takes in a pointer to null-terminated string, then check if the pointer's value is null. If it is, return without doing anything. If it is not, call itself again by passing in the pointer to the next character in the string. Directly after this line of code, remember to print out the pointer's current value (this is important to ensure that your characters are printed in reverse order)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.