473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

replace a word in a line C language

254 100+
i was wondering how to replace a word in a line.

i search in google.com and thescipts.com, i hardly to find what i want.

for example this line,

"I like his daughter"

replace his with "her".

first, i find the substring "him" in the line.
and then replae it with "her" at that position(string address), to let it become,

"I like her daughter"

how am i do that in C language ?
any idea?

i dont give the code here cos what i want is just the code of replacing the word baisically, that i dont know what to code.
-----------
thanks.
Mar 11 '07 #1
3 8831
willakawill
1,646 1GB
First check out this function for finding a string in text and then lookup 'replace' in your c help
Mar 11 '07 #2
questionit
553 512MB
This might be non-professional but a working example of how this could be implemented with comments :

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4.  
  5. void main()
  6. {
  7.  
  8.     char str[20] = "this is big house";
  9.  
  10.     char *wordtoDelete = "big";
  11.     char *replaceWith= "small";
  12.  
  13.     char *temp= "";
  14.  
  15. temp = strstr(str,wordtoDelete);    // got at start of 'word to replace' word
  16.  
  17. //get all which is after the 'word to replce'
  18. char *getafter = temp + strlen(wordtoDelete); 
  19.  
  20. int pos = temp-str;  // start of 'word to delete' position
  21.  
  22. //end the string before the 'word to replace word'
  23. str[pos] = '\0';
  24.  
  25. char newS[50];
  26. strcpy(newS,str);  //copy the string before the 'word to delete'
  27.  
  28. strcat(newS,replaceWith);       //append the word you want to replace
  29.  
  30. strcat(newS,getafter);       // append all which was after the 'word to delete'
  31. puts(newS);
  32.  
  33.  
  34. }
Let me know how you find it.
Mar 11 '07 #3
Banfa
9,065 Expert Mod 8TB
First check out this function for finding a string in text and then lookup 'replace' in your c help
An interesting function that can not possibly work as advertised without introducing undefined behaviour. It mallocs 1 character too few for the buffer it copies to in order to compare against resulting (I guess) in a buffer over-run when it calls Left (either that or it fails to zero terminate the string.

Additionally it is woefully in efficient compared to what could be written in straight C/C++ ignoring trying to make it VB like, which should not need to copy any memory.
Mar 12 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: - ions | last post by:
Hi, i would like to know how to replace every char in a string with a certin given char using the String.replace(char oldChar,char newChar). I would like to replace all letters with an underscore...
3
by: Claude Schneegans | last post by:
Hi, I'trying to use a regExp in Javascript to replace any all upper case word in a string by the same word having only its first letter in upper case. This expression detects the words: /()(+)/g...
3
by: gregpinero | last post by:
I'm trying to write a little script that will have a list of word pairs which will loop through that list and replace all instances of each word with the other word. I'm very new to javascript...
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
18
by: William Payne | last post by:
Hello, I need to write a program that opens a text file and scans the entire file for a specific line and when that line is found, a particular word on that line is to be replaced. The new word is...
1
by: kids_pro | last post by:
Hi there, Is it possible to use RegEx.Replace to replace string like this. string str = "The world heavy weight match"; I want to replace world = w and match = m I can do that with 2 line of...
18
by: Jon S via DotNetMonster.com | last post by:
Hi all, I searching a string to see if "Desc" resides in it, if so then replace it with "Description". For some reason if it finds "Desc" in the word "Description" it replaces the "Desc" part...
5
by: Casey | last post by:
Hello, Can someone give me specific code to replace text on a page using server side javascript? I need to use server-side because I need the output to be recognized in the final HTML so that...
12
by: implement | last post by:
Hi all! I try to code my own version of replace but it doesn't work. I hope somebody can help me out. It only replaces the first char!. Why I don't use the public string.replace function?...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.