Can anyone please help me make a program that does following:

i enter first string
then i enter the second string
i.e.
first string: "abcxyzefg"
second string:"xyze"

and the program should the write a new string without the second string in the first one i.e "abcfg"

thank you

Recommended Answers

All 7 Replies

Buddy a quick warning, post some code first or the board is gonna destroy you, plus its against board regulations to help when you don't put nay effort :p

#include<iostream>

using namespace std;

int main(){
    cout << "Hello World!" << endl;
/*Enter the rest here*/
}

oh ok, I didn't know that...

here's what i did..but it only works partially

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#define n 30
#define m 4

char string1[n];
char string2[m];

char string3[n];
int counter=-1;

void deleteSubstring(char string1[n],char string2[m]){

     int i,j,nn,mm,ind;
     nn=strlen(string1);
     mm=strlen(string2);

     for (i=0; i<nn; i=i+mm) {
         ind=1;
         for (j=0; j<mm; j++){
             if (string1[i+j]!= string2[j]) {
                ind=0;
             }
         }
         if (ind==0) {
             for (j=0; j<mm; j++){
                 counter++;
                 string3[counter]=string1[i+j];
             }
         }
     }

}

int main()
{     int j;
      cout<<"\n Insert string1 ";
      cin>>string1;
      cout<<"\n Insert string2 ";
      cin>>string2);
      if (strlen(string2)>strlen(string1)){
         cout<<"\n Error";
         exit(0);
      }

      deleteSubstring(string1,string2);

           for (j=0; j<strlen(string3); j++)
          cout<<string3[j];

      system("PAUSE");
      return 0;
}

And what exactly is the problem? Although from just glancing I think you should remove #include<stdio.h> as you already are using the iostream library also remove your #define ... and use const int instead its better practise for C++.

Also avoid using global variables and make them part of your main.

the problem is it doesn't work
try entering the first string "windows"
and the second "do"
it won't do anything...can you explain me how to make it work in any example?

Firstly when your getting input of strings you shouldn't use cin use cin.get(a); as the .get is used to accept char input.

Secondly once again I have no idea what the problem is saying "the problem is it doesn't work" doesn't help. As I don't have a compiler on me I need you to tell me whats going wrong.

This is C++, so make your life easier and use the string library, with its find, erase, and replace functions. Find "do" and erase it or replace it with nothing.

What I understand is that you are trying to REMOVE a substring from a string. This is the c++ way.
Simply:

#include <iostream>
#include <string>

using namespace std;
int main ()
{
string mazin;
string inputstring;

cout<<"Enter first string: ";
getline(cin, mazin);

cout<<"Enter second string (to remove): ";
getline(cin, inputstring);


int indexhere = mazin.find(inputstring); //the starting index (zero-based)
int numberofchars =inputstring.length(); //the number of characters to take away
if (indexhere!=-1)
cout<<"Substring removed: "<<mazin.erase(indexhere,numberofchars)<<'\n';
else
cout<<"Could'nt find input string in original string.\n";

}

Now, understand this code and then modify it to your needs.


Using C-style strings requires complicated manipulation. This is why I suggest this.

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.