473,698 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

replace a substring from a string - C

mikewagan
5 New Member
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 2537
Ganon11
3,652 Recognized Expert Specialist
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 Recognized Expert Moderator Expert
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
mikelwagan
6 New Member
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
mikelwagan
6 New Member
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
21690
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 i found no solution. The original code looks like the following: <!-- The replacement-function that i'm calling --> <xsl:template name="replace-string">
5
39647
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 should I do? Thanks
4
3850
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, that the split method and the regex.replace method was better than the string.replace method and replace function. I did not believe that.
10
9601
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 Replacement As String) As String '... End Function
1
6710
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 last, const basic_string& s) I am going to replace the substring specified by first and last and replace it with s, where s may longer or short than the substring being replaced. If the replace function suitable for my case ? Anyway, I use
1
2898
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 fewer than 80 characters. It then prompts the user to type the search-substring of 10 or fewer characters. Finally, it prompts the user to type the replace-substring of 10 or fewer characters. The second function call is the search-and-replace...
8
80274
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? Thanks
9
3787
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 regular expression like: rx = /(?:(\.)|\b)(?:name1|name2|name3)\b/g; map = ; map = "a"; map = "b";
3
3959
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 below and it works fine as long as I'm not adding tags around the to parameter. Can anyone explain to me why it doesn't work with tags? And it needs to be XSLT 1.0. This works: X<xsl:value-of select="'little steak'"/>X This doesn't work:...
0
8678
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7737
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6525
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.