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

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 iamthwee

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.