473,399 Members | 2,858 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,399 software developers and data experts.

replace a substring from a string - C

mikewagan
Hello guys, need help here on C! I need a function to replace a substring inside a string with another new string. Example, we have "you are famous than me", we need to replace "famous" with "more famous", so to get the output "you are more famous than me". How do we do that?

Thanks for your help guys!
Jan 29 '08 #1
4 2517
Ganon11
3,652 Expert 2GB
So you have a string "[some text...][TEXT TO BE REPLACED][more text...]" and you want to replace "TEXT TO BE REPLACED" with "TEXT TO REPLACE WITH". You can do this using the string::substr method and the string::find method. Look at these methods and try to figure out how you can use them.

EDIT: You could do this if it were C++, but this is C. Whoops.

I'm not sure if there's a find function for CStrings, but you will do essentially the same thing - find the text to be replaced (probably manually), write the text before this to a new buffer, write the new text, and write the remaining text to the new buffer.
Jan 29 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
And be sure while you are doing this to not return a heap string that must be deleted nor overrun the stack buffer containing the original string.
Jan 29 '08 #3
Hello guys, need help here on C! I need a function to replace a substring inside a string with another new string. Example, we have "you are famous than me", we need to replace "famous" with "more famous", so to get the output "you are more famous than me". How do we do that?

Thanks for your help guys!
tokayo... hehehe


void replaceStr(char *orig,
char *keyword,
char *repword,
char *out)
{
char *tmpStr = strstr(orig, keyword);

if (tmpStr == NULL)
{
/* wala */

}
else
{
/* use strncpy and strcat. as simple as that */

}

}
Feb 5 '08 #4
implement string functions if your prof doesn't want you to use them. you sure know their functions, right? :D
Feb 5 '08 #5

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

Similar topics

5
by: K.Simon | last post by:
Hello, it's very often neccessary to replace strings or a single character in my stylesheets. My solution looks awful and very long. Now i thought to solve this with an array like structure but...
5
by: mp | last post by:
when I write this in c# : strFileToLoad = strFileToLoad.Substring(0, 2) + strFileToLoad.Substring(2).Replace("\\", "\"); it doesnt like the last "\", and I want to replace \\ with \ what...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
10
by: pamelafluente | last post by:
I need to replace all the occurences of a string within another string (or stringbuilder): Function ReplaceInsensitive(ByVal InputString As String, _ ByVal SubstringReplaced As String, _ ByVal...
1
by: MCH | last post by:
Hi there, I am using std:string and try to replace a substring with replace function. From the document, I found one prototype of string.replace basic_string& replace(iterator first, iterator...
1
by: coolami4u | last post by:
I need a program that simulates the search-and-replace operation in a text editor. The program is to have only three function calls in main. The first function prompts the user to type a string of...
8
by: Johny | last post by:
Let's suppose s='12345 4343 454' How can I replace the last '4' character? I tried string.replace(s,s,'r') where 'r' should replace the last '4'. But it doesn't work. Can anyone explain why?...
9
by: Mark Szlazak | last post by:
I don't think this is "do-able" but thought I'd better check. Say I want to replace certain names in some source code as long as they are not properties (dot properties) of objects. I could use a...
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.