2

I have this code here

#include <stdio.h>
#include <string.h>
#include<conio.h>

void main()
{
  char s[] = "All work and no play makes Jack a dull boy.";
  char word[10],rpwrd[10],str[10][10];
  int i=0,j=0,k=0,w,p;

  printf("All work and no play makes Jack a dull boy.\n");
  printf("\nENTER WHICH WORD IS TO BE REPLACED\n");
  scanf("%s",word);
  printf("\nENTER BY WHICH WORD THE %s IS TO BE REPLACED\n",word);
  scanf("%s",rpwrd);
  p=strlen(s);

  for (k=0; k<p; k++)
    {
      if (s[k]!=' ')
        {
          str[i][j] = s[k];
          j++;
        }
      else
        {
          str[i][j]='\0';
          j=0; i++;
        }
    }

  str[i][j]='\0';
  w=i;

  for (i=0; i<=w; i++)
    {
      if(strcmp(str[i],word)==0)
        strcpy(str[i],rpwrd);

      printf("%s ",str[i]);
    }
  getch();
}

How can I replace the word just 'Jack'? like

Output:

All work and no play makes Jack a dull boy.

Enter ther word Jack to be Replaced
Mark
Tom

All work and no play makes Mark a dull boy.
All work and no play makes Tom a dull boy.

without search the whole Sentence.

thx

5 Answers 5

3

without search the whole Sentence.

You have to search the entire line:

char sentence[] = "The quick brown fox jumped over the lazy dog.";
const char *to_replace = "fox";
const char *replacement = "dragon";

char *pos = strstr(sentence, to_replace);

// if found
if (pos != NULL) {
    // The new string
    size_t newlen = strlen(sentence) - strlen(to_replace) + strlen(replacement);
    char new_sentence[newlen + 1];

    // Copy the part of the old sentence *before* the replacement
    memcpy(new_sentence, sentence, pos - sentence);

    // Copy the replacement
    memcpy(new_sentence + (pos - sentence), replacement, strlen(replacement));

    // Copy the rest
    strcpy(new_sentence + (pos - sentence) + strlen(replacement), pos + strlen(to_replace));

    printf("Old: %s\nNew: %s\n", sentence, new_sentence);
}
2
  • Technically you don't have to search the entire line, just search up until you find the word to be replaced.
    – iain
    Commented Mar 17, 2013 at 20:35
  • @iain And if it's the end, that's the entire line. Assumptions about worst case should be made, and also string processing algorithms (such as this one) run generally in O(n).
    – user529758
    Commented Mar 17, 2013 at 20:39
0

The fastest way would be to allocate a new string that is strlen (s) - strlen (word) + strlen (rpwrd) + 1. Then use the strstr function to find the word to be replaced and copy up to that point into a new string, append the new word, then copy the rest of the original sentence into a new string.

0

You would need to declare a char* and assign it dynamic Memory using malloc or calloc. This appears, when the word would be longer than the replaced word.
Secondly, there are many functions in <string.h> to search a string and replace it by a given string. There is the strstr-function which locates a substring and returns the position inside the string. Although it is about C++, look here for a reference on the <string.>-Header.

1
  • 3
    char s[] = "All work and no play makes Jack a dull boy."; - here, s is not read-only, writing to it is just fine. Also, since C99, there's no need to malloc() for such a simple use case.
    – user529758
    Commented Mar 17, 2013 at 20:29
0
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a simple string made with simple code";
  char * pch;
  int i=0,count=0;
  for(i=0;i<strlen(str);i++){
    if(str[i]=='s'&& str[i+1]=='i'&&str[i+2]=='m'&&str[i+3]=='p' && str[i+4]=='l' && str[i+5]=='e'){
        count++;
      }
  }
  for(i=1;i<=count;i++){
    pch = strstr (str,"simple");
    strncpy (pch,"sample",6);
  }

  puts (str);
  return 0;
}
-1

This might work :

 #include<bits/stdc++.h>
 using namespace std;
 int main()
 {
    char str[] ="xxxxforxxxx xxxx for xxxx";
    int n=sizeof(str)/sizeof(str[0]);
    string old ="xxxx";
    int oldlen=old.length();
    string news ="geeks";
    string final="";
    for(int i=0;i<=n-oldlen-1;i++)
    {
        string check="";
        for(int j=i;j<old.length()+i;j++)
        {
            check+=str[j];
        }
        if(old==check)
        {
           final+=news;
           i=i+oldlen-1;
        }
        else
        {
           final+=str[i];
        }
    }
    final += str[str.Length-1];
    cout<<final;
 }
1
  • 2
    You should add some explanation / comments.
    – m02ph3u5
    Commented Jan 11, 2020 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.