Debugging a C++ Program to Replace Strings

Click For Summary

Discussion Overview

The discussion revolves around debugging a C++ program designed to search for a substring within a string and replace it with another substring. The focus is on identifying issues in the code and understanding debugging practices.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster shares their code and requests assistance in identifying issues.
  • Some participants inquire about the compilation status and whether the poster has used a debugger.
  • There is a suggestion that debugging requires showing the execution process of the program, similar to how physics problems are approached.
  • The original poster later claims to have solved the issue without detailing the solution.
  • One participant questions whether the program handles edge cases effectively, such as specific input strings.
  • The original poster confirms that the program works for the mentioned edge case.

Areas of Agreement / Disagreement

Participants generally agree on the importance of debugging practices, but there is no consensus on the specific issues within the code, as the original poster claims to have resolved their problem independently.

Contextual Notes

There are unresolved questions about the program's behavior in various scenarios, and the original poster does not provide detailed information about the debugging process or the nature of the solution they found.

Who May Find This Useful

Individuals interested in C++ programming, debugging techniques, and string manipulation may find this discussion relevant.

Petkovsky
Messages
61
Reaction score
0

Homework Statement



Ok, i need to make a program that will search for a substring in a given string, and replace it with another one.

The Attempt at a Solution



Code:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;int fsubstitute(char *ulazni, char *prvi, char *drugi);

int main()
{
	char initial[255];
	char find[255];
	char replace[255];

	gets(initial);
	gets(find);
	gets(replace);

	fsubstitute(initial,find,replace);
	
	
}

int fsubstitute(char *initial, char *find, char *replace)
{
    char *temp1;
       
    int i,j,k,c,l,n;
    i = j = k = c = l = n = 0;
    
    
    n = strlen(find);
    l = strlen(initial);
    char temp2[200];
   
    do{
        
        
        if(strstr(initial,find)!= NULL)
        {
        temp1 = strstr(initial,find)+ n; 
       
        for(i=0;i<l-strlen(temp1)-n;i++)
        temp2[i] = *(initial+i);
        temp2[i] = '\0';
        
        initial = temp2;
        
        initial = strcat(initial,replace);
        initial = strcat(initial,temp1);
        
        }
    }while(strstr(initial,find)!= NULL);
    cout << initial << endl;
    
    return 0;
};

I used cout since it is easier to write, and that's why i included a c++ library/

Can you tell me what's wrong with my code?
 
Physics news on Phys.org
Does it compile? If not, what is the message?

If it does compile, does it compile clean with all warnings enabled?

Have you used a debugger? If so, where did your code behave other than you expected?

If you haven't used a debugger, why not?
 
D H said:
Does it compile? If not, what is the message?

If it does compile, does it compile clean with all warnings enabled?

Have you used a debugger? If so, where did your code behave other than you expected?

If you haven't used a debugger, why not?

I mean everything's fine with the syntax, it just won't do its job.
 
So, have you debugged your program? Do you even know what that means?

We treat computer programming problems here the same as physics homework problems: You have to show your work. In the case of a computer program, showing your work means you telling us a bit about the execution of your program. We don't do physics homework for students because that is a disservice. The same goes for us debugging your program for you.
 
I solved it, thanks for the help anyway.
 
Does your program work for nasty cases, for example initial="this is a test", find="is", replace="this"?
 
Yes it does :)
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 24 ·
Replies
24
Views
4K