#include #include // There is a more subtle bug in this implementation // of a function meant to copy a C-style string from // one character array to another. // // Try running this code with a few inputs. Depending // on what you enter, you may not immediately encounter // the bug. Try several different inputs. // // Determine what the problem is and fix it. // Function declarations void str_copy( char const orig[], char copy[] ); int main(); // Function definitions int main() { char first[1000]; char second[1000]; for ( std::size_t k{ 0 }; k < 2; ++k ) { std::cout << "Enter a string: "; std::cin >> first; str_copy( first, second ); std::cout << "The string you entered was \"" << first << "\"." << std::endl; std::cout << "The copy of what you entered is \"" << second << "\"." << std::endl; } return 0; } // void str_copy( char const orig[], char copy[] ) // // @param orig A C-style string: a null-character- // terminated character array. // @param copy A character array capable of holding // the entire string plus the null // character. // // When this function exits, the second argument // contains a C-style string with all the characters // copied from the first argument. void str_copy( char const orig[], char copy[] ) { for ( std::size_t k{ 0 }; true; ++k ) { if ( orig[k] == '\0' ) { return; } else { copy[k] = orig[k]; } } } // If you have not yet found the bug, try entering // two different strings. // // First enter a longer string, and then enter a // shorter string. // // What happens? // For example, first enter the 8-character string // // Welcome! // // Then, the second time it asks, enter the shorter // 3-character string // // Hi! // // - What is the output after the second input? // - Why is this happening? // Hopefully you have now found the problem. // // When the null character is found in orig, the // function immediately returns. Consequently, the // null character is never copied into copy. // // For example, after copying // // Welcome! // // the array copy should begin with // // 'W' 'e' 'l' 'c' 'o' 'm' 'e' '!' '\0' // // Now suppose we copy // // Hi! // // into the same array. The buggy function changes // only the first three entries: // // 'H' 'i' '!' 'c' 'o' 'm' 'e' '!' '\0' // ^ // // It then encounters the null character in orig // and returns without copying it. // // Thus, copy still contains characters left over // from the previous string. When std::cout prints // copy, it continues until it eventually encounters // the old null character. // // The correct result should instead be // // 'H' 'i' '!' '\0' // // The fix is simple. On each iteration: // // 1. Copy the kth character from orig to copy. // // 2. If that character was the null character, // return because the entire C-style string, // including its terminator, has been copied. // Here is the corrected function: /* void str_copy( char const orig[], char copy[] ) { for ( std::size_t k{ 0 }; true; ++k ) { copy[k] = orig[k]; if ( orig[k] == '\0' ) { return; } } } */