Hello, this is my first post. I need help with this assignment here, dont know where to begin! maybe I should use strstr()? The output needs to be what is shown at the bottom. I cant use the string class, only c-style functions. Any hints would be welcome thank you!

#include <iostream>
#include <fstream>
#include <cstring>

            using namespace std;

            //Prints the characters in the string str1 from beginIndex
            //and including endIndex
            void printSubString( const char* str1 , int beginIndex, int endIndex )
            {

                //code needed
            }

            void printWordsInAString( const char* str1 )
            {

               //code needed

            }

            int main()
            {
            char str1[] = "The fox jumps over the fence." ;
            char str2[] = "The              fox jumps over the fence." ;
            char str3[] = "    The fox jumps over the fence." ;
            char str4[] = "The fox jumps over the fence.    " ;

            printWordsInAString( str1 ) ;
            cout << endl ;
            printWordsInAString( str2 ) ;
            cout << endl ;

            printWordsInAString( str3 ) ;
            cout << endl ;
            printWordsInAString( str4 ) ;
            cout << endl ;

            return( 1 ) ;
            }

Output:

The:fox:jumps:over:the:fence.:
The:fox:jumps:over:the:fence.:
The:fox:jumps:over:the:fence.:
The:fox:jumps:over:the:fence.:

Dani AI

Generated

Good goal: print each word from the C string followed by a colon, compressing any runs of whitespace and ignoring leading/trailing spaces. Combine ’s two-rule idea (trim leading spaces, collapse multiple spaces) with ’s single-pass “in-word” approach — a simple scanner that finds start/end indices for each word and calls the helper that prints that substring.

Key points to follow before dropping into code: skip any initial whitespace, treat any run of whitespace as a single separator, and print a colon after every word (including the last) to match the shown output. Prefer C library whitespace testing (std::isspace) if tabs or other whitespace should be handled; otherwise checking for ' ' is fine. Be careful with inclusive end indices (off-by-one) and always check for the null terminator.

Here are compact implementations for the two missing functions (requires #include <cctype> if using std::isspace):

void printSubString(const char* str1, int beginIndex, int endIndex)
{
    for (int i = beginIndex; i <= endIndex && str1[i] != '\0'; ++i)
        std::cout << str1[i];
}

void printWordsInAString(const char* str1)
{
    int i = 0;
    int len = std::strlen(str1);

    // skip leading whitespace
    while (str1[i] && std::isspace((unsigned char)str1[i])) ++i;

    while (str1[i]) {
        int start = i;
        // advance to end of word
        while (str1[i] && !std::isspace((unsigned char)str1[i])) ++i;
        int end = i - 1;
        if (start <= end) {
            printSubString(str1, start, end);
            std::cout << ':';
        }
        // skip whitespace between words
        while (str1[i] && std::isspace((unsigned char)str1[i])) ++i;
    }
}

Troubleshooting tips: cast to unsigned char before std::isspace to avoid UB; test with strings of only spaces (should print nothing); watch the inclusive endIndex in printSubString; and if the program still misbehaves, dump intermediate indices (start/end) to verify word boundaries.

Recommended Answers

All 2 Replies

Actually I see TWO RULES to be applied in code to the strings.

  1. If there are leading spaces, remove them,.
  2. If there are more than one space, leave one space.

So you'll need two loops, one for each rule. Yes I bet you could use regular expressions but then you have a new problem.

As a first attempt I suggest one loop that steps through the original string char by char as well as a boolean that you can set to true or false depending on whether or not you are within a word. Set it to true if the char is a letter and false if it is not. While the switch is true you collect each letter in the new word. When it gets set to false you print the just completed word.

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.