Jump to content
  • Advertisement

Archived

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

penetrator

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

This topic is 5806 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
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
#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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!