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

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.