2

I'm trying to insert a string into another string. when I find the first apparition of the user defined character, and inserting the string after the first position of the character and I can't get the program to take ' '(space) as a character not as a separator.

#include <iostream>
#include <math.h>
#include <fstream>
#include <strings.h>

using namespace std;

int main()
{
    int i=0,j=0,ok=0,k=0,p=0;
    char s[256],aux[256],a[256];
    char c;
    cin.get(s,256);//reading the string
    cin.get();
    cin.getline(a,256);//reading the string that I want to insert
    cin.getline();
    cin>>c;//reading the separator
    strcpy(aux,s);
    while(j<=strlen(s) && s[j]!=c)//searching for the first apparition of the separator
    {
        j++;
    }
    ok=strlen(a);
    strcpy(s+j+1,a);//making room for the string that insert and inserting the string
    strcpy(s+j+ok,aux+j);
    cout<<s;
}
jww
  • 97,681
  • 90
  • 411
  • 885
  • 3
    Possible duplicate of [std::cin input with spaces?](https://stackoverflow.com/questions/5838711/stdcin-input-with-spaces) – Matthieu Brucher Dec 04 '18 at 10:14
  • 2
    What is the input you give your program? What is the expected and actual output? Have you tried stepping through the program in a debugger to see the contents of all arrays and variables you have? And please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Dec 04 '18 at 10:14
  • Have you tried debugging this and seeing that what you are trying to insert as a separator is actually inserted? You are executing multiple input functions and it can become confusing. Let's debug this and see what you get, I'm here if you have questions. – Oren_C Dec 04 '18 at 10:16
  • 2
    `strings.h` is not a part of C++. It's a POSIX library. Please use `std::string` with `#include ` rather than legacy C-style character arrays – n. m. could be an AI Dec 04 '18 at 10:18
  • I tried debugging it, and I inserted the libray. The program works just fine with other characters as c variable but it does nothing with " "(space). – Alexandru Gheorghe Dec 04 '18 at 10:30

2 Answers2

0

First of all you have to include library "string.h" in order to compile your program. Then, you can do as follow:

#include <iostream>
#include <math.h>
#include <fstream>
#include <strings.h>
#include <string.h>
using namespace std;

int main()
{
    int i=0,j=0,ok=0,k=0,p=0;
    char s[256],aux[256],a[256];
    char c;
    cin.get(s,256);//reading the string
    cin.get();
    cin.getline(a,256);//reading the string that I want to insert
    c = getchar();//reading the separator
    getchar(); // for getting enter from keybord
    strcpy(aux,s);
    int len = strlen(s); // this function is of time complexity O(len)
    while ( j <= len && s[j]!=c)//searching for the first apparition of the separator
    {
        j++;
    }
    ok=strlen(a);
    strcpy(s+j+1,a);//making room for the string that insert and inserting the string
    strcpy(s+j+1+ok,aux+j);
    cout<<s;
}

Note: Instead of calling strlen(s) method again and again inside while loop, you can simply calculate length of string - s once. otherwise, it will increase time complexity.

BishalG
  • 1,414
  • 13
  • 24
0

Generally space we take as separator,I think it will also have ASCII value. please check sample code i am inserting space just like any other character. if i find character 'e', i am inserting space. we also have isspace function to check for space in C++.

char str[]="Examplesentence to test isspace\n";
while (str[i])
{
c=str[i];
if (c =='e' )  c=' ';
putchar (c);
}