Need help

I'm doing a lab question I couldn't get in my lab class today, but having difficulties even starting. Can I get some help?

Question:
1- Write a function that converts its String parameter so that letters are written in blocks five letters long. For example, consider the following two versions of the same sentence:

Plain: This is how we would ordinarily write a sentence.
Blocked : Thisi showw ewoul dordi naril ywrit easen tence


My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;

void blocks( const char * const sPtr);

int main ()
{
    char sentence[40];
    printf("Enter a sentence:");
    gets(sentence);
    
    printf("Sentence in blocks of 5 is:");
    blocks( sentence);
}

void blocks( const char * const sPtr)
{
    string s;
    for (int i = 0; i < s.length(); i++)
        
}
I will leave it up to you on how to remove the period at the end of your final output. AND how to implement this as function. Feel free to ask questions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * 1- Write a function that converts its String parameter
 * so that letters are written in blocks five letters long.
 * For example, consider the following two versions of the same sentence:
 *
 * Plain: This is how we would ordinarily write a sentence.
 * Blocked : Thisi showw ewoul dordi naril ywrit easen tence
 *
 */

#include <iostream>
#include <bits/stl_algo.h>

using namespace std;

int main() {
    string input = "This is how we would ordinarily write a sentence.";

    // algorithm
    // get input
    // remove spaces
    // add space every 5th letter


    // REMOVE SPACES FROM STRING
    // http://stackoverflow.com/questions/16329358/remove-spaces-form-a-string-c
    input.erase (std::remove (input.begin (), input.end (), ' '), input.end ());

    // print test //
    cout << input << endl;


    // Add space every fifth char
    //http://stackoverflow.com/questions/9438209/for-every-character-in-string

    int i = 0;
    for (char &c : input) {
        i++;
        if (i > 5) {
            cout << " " << c;
            i = 0;
        } else {
            cout << c;

        }

    }
    return 0;
}



Thisishowwewouldordinarilywriteasentence.
Thisi showwe wouldo rdinar ilywri teasen tence.
Last edited on
Thank you, it helped a lot and the links you provided really made me understand it. But I have one question. How come you used the header #include <bits/stl_algo.h> I changed it to stdio.h and it still worked, so was wondering out of curiosity.
<bits/stl_algo.h> is and gcc implementation header which should not be used directly.
Correct header to include when using std::remove is <algorithm>
(Also you need <string> for std::string)

it still worked
Standard library headers can include others, but it is not required and if you are relying on it, your program may not work on different compilers or break after compiler update. Example: all compilers include bits of <string> (they need it for several stream operation), but Micrisoft implementation of standard library does not include template alias std::string for std::basic_string<char>, so attempt to declare a std::string variable without including corresponding header are doomed to failure.

EDIT: it appears tha MS implementation does include std::string alias, but leaves out all non-member functions, like to_string and stream operators.
Last edited on
@thatfunnydude what MiiNiPaa said is true.

you can see here that algorithm (below) makes one call to the implementation of bits/stl_algo.h among a bunch of other includes. So not a good idea to use in general.

https://gcc.gnu.org/onlinedocs/gcc-4.6.2/libstdc++/api/a00746_source.html


Topic archived. No new replies allowed.