Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8780939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:10:47+00:00 2026-06-13T20:10:47+00:00

In Programming Pearls: Strings of Pearls, section 15.3 (Generating Text), the author introduces how

  • 0

In “Programming Pearls”: Strings of Pearls, section 15.3 (Generating Text), the author introduces how to generate random text from an input document. In the source code, there are some things that I don’t understand.

for (i = 0; i < k; i++)
        word[nword][i] = 0;

The author explains: “After reading the input, we append k null characters(so the comparison function doesn’t run off the end).” This explanation really confuses me, since it still works well after commenting these two lines. Why is this necessary?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:10:48+00:00Added an answer on June 13, 2026 at 8:10 pm

    Doing that reduces the number of weird cases you have to deal with when doing character-by-character comparisons.

     alphabet
     alpha___
    

    If you stepped through this one letter at a time, and the null padding at the end of alpha wasn’t there, you’d try to examine the next element… and run right off the end of the array.
    The null padding basically ensures that when there’s a character in one word, there’s a corresponding character in the other. And since the null character has a value of 0, the shorter word always going to be considered as ‘less than’ the longer one!

    As to why it seems to work without those lines, there’s two associated reasons I can think of:

    1. This was written in C. C does not guard its array boundaries; you can read whatever junk data is beyond the space that was allocated for it, and you’d never hear a thing.
    2. Your input document is made such that you never compare two strings where one is a prefix of the other (like alpha is to alphabet).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is code from programming pearls this code prints random numbers in decreasing form
Here are functions on strings from programming pearls. int wordncmp(char *p, char* q) {
I have this code from programming pearls #include <iostream> //#include <string> using namespace std;
i have implement this code from programming pearls and i think it should be
I have to implement quicksort. From Programming Pearls here is the code: public class
The following quicksort code comes from programming pearls void qsort3(int l, int u) {
From Programming Pearls : Column 12: A Sample Problem : The input consists of
From Programming Pearls: Column 12: Searching: There are two code segments: IntSetList(int maxelements, int
Hey, in the Programming Pearls book, there is a source code for setting, clearing
Here is a problem from Programming Pearls 2nd edition (Chapter 8.7): Considering a real

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.