• Advertisement

Archived

This topic is now archived and is closed to further replies.

OT: search and replace chars in string with C++

This topic is 5512 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In VB i''d use a simple Replace function, but i''m new to C++ so excuse me for all these questions ... I''d like to search a string and replace chars. This is my code, but i get several errors when i compile, can you help me ? char kill[6]; strcpy(kill, "------"); char replace[2]; strcpy(replace, "--"); for (int i=0; i
www.web-discovery.net

Share this post


Link to post
Share on other sites
Advertisement
I''m tryng to read single chars from a string, buti get errors. Take a look at this code:

char project_geo_temp[7];
char singlechars[7][1];
strcpy(project_geo_temp, "Testing";

z=strlen(project_geo_temp);

for (int i=0; i{
sscanf(project_geo_temp, "%1c", singlechars[i]);
}

What i would like to get is an array like :

singlechars[1] = T;
singlechars[2] = e;
singlechars[3] = s;
singlechars[4] = t;
singlechars[5] = i;
singlechars[6] = n;
singlechars[7] = g;


Share this post


Link to post
Share on other sites
quote:
char singlechars[7][1];


you declared singlechars as a 2 dimensional array. Therefore you have to acess it 2 dimensionally(ie singlechars[1][0], singlechars[2][0]).

It seems point less to have arrays of size one tho. What you have done is created an array of 7 arrays of size 1. It''s rather pointless to have an array of size one so you should just use singlechars[7].

Share this post


Link to post
Share on other sites
for (int i=0; i{
sscanf(project_geo_temp, "%1c", singlechars);
}

.
/ \
|
|

That doesn''t look like a correct for loop syntax.

Share this post


Link to post
Share on other sites
no, sorry that''s a type, the code is:

char project_geo_temp[7];
char singlechars[7][1];
strcpy(project_geo_temp, "Testing";

z=strlen(project_geo_temp);

for (int i=0; i{
sscanf(project_geo_temp, "%1c", singlechars);
}

What i would like to get is an array like :

singlechars[1] = T;
singlechars[2] = e;
singlechars[3] = s;
singlechars[4] = t;
singlechars[5] = i;
singlechars[6] = n;
singlechars[7] = g;


Share this post


Link to post
Share on other sites
Finding and replacing chars in a string with c++:

string s = "something foo something";
int position = s.find("foo");
s.replace(position, 3, "pie");


If the substring is not found, then find will return string::npos - be sure to check for this when necessary. By the way, this topic might have been better off in the general programming forum.

Share this post


Link to post
Share on other sites
Yes, i should have posted this in the programming forum. Which header do i need to use string replace funtion ?

Share this post


Link to post
Share on other sites
#include <string> (no .h) allows you to use the std::string type. If you only want to type string instead, then also add the line using std::string; or using namespace std; after the #include.

Share this post


Link to post
Share on other sites
these are the errors shown when i include

:\programmi\microsoft visual studio\vc98\include\ostream(106) : error C2535: ''class std::basic_ostream<_E,_Tr> &__thiscall std::basic_ostream<_E,_Tr>::operator <<(int)'' : member function already defined or declared
c:\programmi\microsoft visual studio\vc98\include\ostream(66) : see declaration of ''<<''
c:\programmi\microsoft visual studio\vc98\include\ostream(272) : see reference to class template instantiation ''std::basic_ostream<_E,_Tr>'' being compiled
c:\programmi\microsoft visual studio\vc98\include\ostream(106) : error C2535: ''class std::basic_ostream > &__thiscall std::basic_ostream >::operator <<(int)'' : member function alr
eady defined or declared
c:\programmi\microsoft visual studio\vc98\include\ostream(66) : see declaration of ''<<''
c:\programmi\microsoft visual studio\vc98\include\ostream(373) : see reference to class template instantiation ''std::basic_ostream >'' being compiled
c:\programmi\microsoft visual studio\vc98\include\ostream(106) : error C2535: ''class std::basic_ostream > &__thiscall std::basic_ostream >:
:operator <<(int)'' : member function already defined or declared
c:\programmi\microsoft visual studio\vc98\include\ostream(66) : see declaration of ''<<''
c:\programmi\microsoft visual studio\vc98\include\ostream(379) : see reference to class template instantiation ''std::basic_ostream >'' being compiled
c:\programmi\microsoft visual studio\vc98\include\istream(103) : error C2535: ''class std::basic_istream<_E,_Tr> &__thiscall std::basic_istream<_E,_Tr>::operator >>(int &)'' : member function already defined or declared
c:\programmi\microsoft visual studio\vc98\include\istream(67) : see declaration of ''>>''
c:\programmi\microsoft visual studio\vc98\include\istream(423) : see reference to class template instantiation ''std::basic_istream<_E,_Tr>'' being compiled
c:\programmi\microsoft visual studio\vc98\include\istream(103) : error C2535: ''class std::basic_istream > &__thiscall std::basic_istream >::operator >>(int &)'' : member function a
lready defined or declared
c:\programmi\microsoft visual studio\vc98\include\istream(67) : see declaration of ''>>''
c:\programmi\microsoft visual studio\vc98\include\istream(544) : see reference to class template instantiation ''std::basic_istream >'' being compiled
c:\programmi\microsoft visual studio\vc98\include\istream(103) : error C2535: ''class std::basic_istream > &__thiscall std::basic_istream >:
:operator >>(int &)'' : member function already defined or declared
c:\programmi\microsoft visual studio\vc98\include\istream(67) : see declaration of ''>>''
c:\programmi\microsoft visual studio\vc98\include\istream(564) : see reference to class template instantiation ''std::basic_istream >'' being compiled
Error executing cl.exe.

fly.obj - 6 error(s), 0 warning(s)


Share this post


Link to post
Share on other sites
your including the headers multiple times.

ie, in one header, say, "files.h" you might include ostream.h

but in another, you may include ostream.h and files.h... this screws somethings up.
and is exactly what precompiled headers avoid (and a whole lot more)


anyway.

to find and replace in a string is easy,

see:


void FileIO::findReplace(char * string, char find, char replace)
{
int ref=-1;

while (string[++ref])
if (string[ref]==find)
string[ref]=replace;
}


| - Project-X - my mega project.. big things comming soon - | - adDeath - an ad blocker I made - | - email me - |

[edited by - RipTorn on January 20, 2003 8:54:51 AM]

Share this post


Link to post
Share on other sites
No, including files multiple times won''t cause that - that''s what inclusion guards are guarding against.
I can only guess at the problem here, but are you including <iostream.h>? Change that to <iostream>. The former is several years out-of-date.

Share this post


Link to post
Share on other sites

  • Advertisement