Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9268779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:56:46+00:00 2026-06-18T14:56:46+00:00

Code I’m using: char** list char** final; char* target; char* replace; int wCounter, cCounter,

  • 0

Code I’m using:

char** list  
char** final;
char* target;
char* replace;
int wCounter, cCounter, i, hashCounter = 0, addLetter = 0;
int copyWord, countChars, numOfWords, finalWords = 0, temp;

//stuff here

for(wCounter; wCounter < temp + numOfWords; wCounter++, finalWords++)
{
    printf("Original string: %s\n", list[wCounter+1]);
    final[finalWords] = strstr(list[wCounter+1], target);

    if(final[finalWords] != NULL)
        memcpy(final[finalWords], replace, strlen(target));

    printf("Final string: %s\n\n", final[finalWords]);
}

//stuff here

My program output:

Target string = h   Replacement string = j

Original string: hello
   Final string: jello //works!!!

Original string: happy birthday
   Final string: jappy birthday  // should be jappy birtjday

Target string = ra  Replacement string = ar

Original string: radar
   Final string: ardar   //works!!!

Original string: are you ready
   Final string: (null)  //awkward

Original string: pirate radio rating
   Final string: arte radio rating //should be piarte ardio arting

Target string = x   Replacement string = zz

Original string: exit
   Final string: zit   //should be zzit

Original string: x-ray
   Final string: z-ray  //should be zz-ray

Original string: xerox
   Final string: zerox //should be zzerox

My program has a target string (‘h’ for example)
It also has a replacement string(‘j’ for example)

everytime it sees an ‘h’ it should replace it with a ‘j’

happy birthday should be jappy birtjday.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:56:47+00:00Added an answer on June 18, 2026 at 2:56 pm

    Ok, there are a several issues with your code.

    First of all: strstr returns only the pointer to the FIRST occurence of the sequence to match (see here for details). So you never get the pointers to the second or third occurence. You need to rework your concept here.

    Secondly: Using memcopy to replace something is only working if the replacement is of the same length as the part to be replaced. Consider following example:

    Address:  0123456789ABCD
    Original: This is a test
    Target: is
    Replacement foo
    

    Notice that the replacement exceeds the length of the target. Now what happens in your code? strstr returns address 2. You pass now this pointer to memcpy. What you get is the following string: Thfo is a test

    Notice that the second ‘o’ of ‘foo’ is missing. This is because you only copy as many bytes as the target string has. If you would have copied as many bytes as the replacement has, you would have ended up with a string like Thfoois a test. Barely better isn’t it?

    I’d recommend to think again about your solution concept. Maybe a better approach is to “tokenize” the string by the occurence of your target string and concatenate the elements in order to get the desired result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code: #include<iostream> using namespace std; void foo() throw(char) {throw 'a';} int main() try {
Code: public IList<T> GetByMultipleIds(int[] ids) { List<T> result = _session.Linq<T>() .Where(x => ids.Contains(x.Id)).ToList(); return
Code: void w(char* c, int i) { char * t; sprintf(t, %d, i); perror(c);
code: char *m[10]; char * s; int lcounter=0; int sd=0; char mem_buf [ 500
Code to create new form instance of a closed form using form name I
Code required to append list (anotherList) to item (id -= l1) of second list
Code looks like public void option() { seeTasks = tasks.getTasks(); for (int i =
Code sample: for file in files: do_something(root+file) I want to replace file with f
Code in C: typedef struct{ int a; int b; }NODE; NODE *node; NODE* add(int
Code on the android device is: public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.