plz i want your help.

if i wrote a string and the user wanted to change a word in it.

sample run:

the sentence is;

my name is gladiator

what word do you change?

gladiator

to?
gladiator1919

the new sentence is :

my name is gladiator1919

Dani AI

Generated

Short answer: in C there is no std::string::find/replace like mentioned for C++; do it manually with strstr (or tokenization) and rebuild the result. Also note 's idea of splitting into words is valid, but avoid the unsafe functions shown in that sketch — and is right: never use gets or unchecked scanf for line input. Use fgets, trim the newline, then search and construct a new string (or allocate one) safely.

The snippet below replaces all occurrences of a substring with another substring and returns a malloc'd result (caller must free it). It shows a safe input pattern using fgets; it performs substring replacement (not "whole-word only"). For whole-word or case-insensitive behavior, add boundary checks or normalize case before searching.

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

/* returns malloc'd string with all occurrences of oldw replaced by neww */
char *replace_all(const char *s, const char *oldw, const char *neww) {
    if (!s) return NULL;
    if (!oldw || !*oldw) { char *r = malloc(strlen(s)+1); if (r) strcpy(r,s); return r; }
    size_t sl = strlen(s), ol = strlen(oldw), nl = strlen(neww);
    const char *p = s; size_t count = 0;
    while ((p = strstr(p, oldw))) { count++; p += ol; }
    size_t newlen = (nl >= ol) ? sl + count*(nl-ol) : sl - count*(ol-nl);
    char *res = malloc(newlen + 1); if (!res) return NULL;
    char *dst = res; const char *src = s;
    while ((p = strstr(src, oldw))) {
        size_t prefix = p - src;
        memcpy(dst, src, prefix); dst += prefix;
        memcpy(dst, neww, nl); dst += nl;
        src = p + ol;
    }
    strcpy(dst, src);
    return res;
}

/* Example usage: read a sentence and two words with fgets, then call replace_all */

Notes and tips: always check return values, free the returned string, and adjust buffer sizes for expected input. To restrict to whole-word replacements, verify characters before/after each match are non-alphanumeric (or use isalnum/ispunct checks). For case-insensitive replace, search in lowercase copies and map indices back to the original.

Recommended Answers

All 6 Replies

what is the code to do this?

You can use find and replace function of string class.

how can i call find and replace function from the string class

Member Avatar for Member #46692

how can i call find and replace function from the string class

by utilising google?

C.... code.. its up for you to translate to C++
and correct other situations.. like no string is entered....

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

#define MAX_LEN_WORD 80
#define MAX_NO_WORDS 100

main(){
   char buffer[MAX_LEN_WORD]="";
   char word[MAX_NO_WORDS][MAX_LEN_WORD];
   int word_counter = 0;

    printf("Enter string");
    gets(string);
/*separating the words */
    len = strlen(string);
    for(x=0;x<len;x++){
         if(string[x]==' '){
          strcpy(word[word_counter],buffer);
          word_counter++;
         buffer = "";
     }
     buffer[x] = string[x];
  }

 printf("What word would you like to change");
  scanf("%s",&change);

/*looking fo that word..  */
  for(x=0;x<word_counter;x++){
     if(strcmp(change,word[x])==0){
           strcpy(word,change);
       }
  }

/*concatening the words back */
for(x=0;x<word_counter;x++){
    strcat(final_sentence,word[x]);
}
/*printing the sentence back..*/

puts(final_string); 
getch();

}

those codes of mine are not tested by the way... just try reading the flow of it... dont just copy the code directly to your program... anyways.. i assume you are not new to programming....

main(){

Wake up and smell the current millenium, or even the '90s.

gets(string);

I know of no forums that will treat you any better than a first-day newb for posting code contiaining the most well-known do-not-use-ever function in C.

scanf("%s",&change);

http://www.daniweb.com/tutorials/tutorial45806.html

getch();

The nonportable code tends to get panned if there is a reasonable and portable replacement.

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.