Jump to content
  • Advertisement
Sign in to follow this  
someone somewhere

A real problem!

This topic is 4518 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

void replace_string_with_stars(char*str)// i must replace the string with 5 stars { char arr[60]; strcpy(arr,str);//lets assume that str is (aaahftgaaahftdsgaaa) cout<<"enter astring"<<endl; while(cin.get()!='\n') continue; char s2[20];//s2 would be aaa cin.getline(s2,20); int c=strlen(s2); char *q=strstr(arr,s2);// im searching for string 2 in the array while(q!=NULL){ if (c<5){// if string length is less than 5 for(int i=strlen(arr);i>arr[q+(c-1)];i--)// i just need away to let the pointer tell me its position in the array arr[i+(5-c)]=arr; for(int j=q;j<6;j++) arr[j]='*'; } else if(c>5){ for(int i=q+4;i<strlen(arr);i++) arr=arr[i+(c-5)]; for(int j=q;j<6;j++) arr[j]='*'; } else if(c=5){ for(int j=q;j<6;j++) arr[j]='*'; } q=strstr(arr,s2); } cout<<arr<<endl; } any ideas will be helpfull.

Share this post


Link to post
Share on other sites
Advertisement
Any ideas? You haven't told us the problem. As for ideas, post your code inside the [ code ] or [ source ] tags (without spaces). Also you are using C++ so there is no reason to use char arrays/pointers for strings.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <string>
using namespace std;

void replace_string_with_stars(string& s1)
{
string s2;
getline(cin,s2,'\n');
basic_string <char>::size_type pos,off=0;
pos=s1.find(s2,off);
while(pos!=string::npos)
{
off=pos+5;//We do not want to get an infinite loop if someone tries to replace “*" with “*****".
s1.replace(pos,s2.length(),"*****");
pos=s1.find(s2,off);
}
cout<<s1<<endl;
}





Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!