How to add a space into character Array?

I have a character Array 'Hello'
I want to add a space in whatever space the users chooses.
"Where do you want to add space? --> " 3
String after space is added --> 'Hel lo

Last edited on
Post what you have.
btw character array looks like this
{'H','e','l','l','o'}
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
void InsertBlank (char S[ ], int  Pos)
{

        char Str[MAX_LENGTH] = "";
        int Position;             //Place in string to insert blank
        
        cout << "\n\n========== Insert Blank Function ===============\n\n";
        cout << "Enter null string to terminate\n";
        
        cin.ignore(100, '\n');  // Eat newline left over from main() input
        
        cout << "\nEnter string => ";
        cin.getline(Str, MAX_LENGTH);          // Read string, discard '\n'
        
        while ( Str[0] != 0 )                 // While null string not entered.
        {
            cout << "\nEnter the position to insert blank ---> ";
            cin >> Position;
            cout << "\nStr = '" << Str << "'\n";
            
            InsertBlank(Str,Position);
            
            cout << "\nAfter calling InsertBlank,\n";
            cout << "\nStr = '" << Str << "'\n\n";
            
            cout << "----------------------------------------------------------\n";
            cout << "\nEnter string => ";
            cin.ignore(100, '\n');               // Gets rid of newline from before
            cin.getline(Str, MAX_LENGTH);        // Read string, discard '\n'
        }
        
        cout << endl << endl;
    
}
Last edited on
Holy recursive abuse, Batman.

Try the following out:
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
#include <iostream>
#include <algorithm>

void insert_blank(char* line, std::size_t line_size, std::size_t pos)
{
    std::size_t index = 0;
    while (line[index++])
        ;

    if (index + 1 >= line_size || pos >= index) // sanity check
        return;

    while (index != pos)
    {
        line[index] = line[index - 1];
        --index;
    }

    line[index] = ' ';
}

std::istream& get(std::istream& is, int& val)
{
    // get a number and remove the trailing newline.
    return (is >> val).ignore(100, '\n');
}

int main()
{
    const char* prompt = "Enter a string (or just hit <ENTER> to quit)\n> ";

    const std::size_t buffer_size = 1024;
    char line_buffer[buffer_size];

    while (std::cout << prompt && std::cin.getline(line_buffer, buffer_size-1) && line_buffer[0])
    {
        const char* prompt = "Enter the position to insert blank (or negative to quit)\n> ";

        int position;
        while (std::cout << prompt && get(std::cin, position) && position >= 0)
        {
            insert_blank(line_buffer, buffer_size, position);
            std::cout << "modified: \"" << line_buffer << "\"\n";
        }
    }
}


http://ideone.com/2QdBVh
thank you for your help. i am still learning everything about c++ and am slowly going through your code to figure it all out.
Topic archived. No new replies allowed.