×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Replacing a character in a string

Replacing a character in a string

Replacing a character in a string

(OP)
I need to write a function that will search for a character in a string and replace all occurances of it with another character

Thanks in advance

RE: Replacing a character in a string

Hwo are you creating your string, thought char or string? For a character array and most strings, the easiest way is to access an individual character is using the brackets, i.e. [ ]. For example,

char A[10];
.
.
.
for(int x = 0; x<10; ++x)
{
    char Z = A[x];
.
.
.
}


It would be best if x was set to 1 less than the length of the character array. Most string functions can be don the same way. I've also seen string libraries that will search the string for a character and replace it with something else but I suppose that is what you eare trying to do on you own.



James P. Cottingham
 
www.ivcusa.com
All opinions are mine alone and do not necessarily reflect those of my employer.

RE: Replacing a character in a string

(OP)
that helps but not for what I need.  I have to be able to search for a spacific char lik say all occourances of the char '9' and replace them with a '4'..

RE: Replacing a character in a string

What you want to is take the char Z and look at what it is. If Z = '9', then A[x] = '4', or something like that.


James P. Cottingham
 
www.ivcusa.com
All opinions are mine alone and do not necessarily reflect those of my employer.

RE: Replacing a character in a string


// before--> character to replace
// after --> character replacing
// pstring --> string sampled
char* replace(char before, char after, char *pstring) {
   for(i=0;i<strlen(pstring);i++) {
      if(pstring[i] == before)
         pstring[i] = after;
   }
   return pstring;
}

RE: Replacing a character in a string

// Cvert1.cpp Converts text files

#include <iostream.h>
#include <fstream.h>

main(int argc, char *argv[])
{
  if(argc!=3) {
    cout << "USAGE: CVERT1 <input-filename> <output-filename>\n";
    return 1;
  }

  ifstream fin(argv[1]);
  ofstream fout(argv[2]);
  if(!fout) {
    cout << "Cannot open output file!\n";
    return 1;
  }
  if(!fin) {
    cout << "Cannot open input file!\n";
    return 1;
  }

  char ch;

  fin.unsetf(ios::skipws);
  while(!fin.eof()) {
    fin >> ch;
    if(ch==' ') ch = '@';
    if(ch=='A') ch = '~';
    if(ch=='B') ch = '|';
    if(ch=='C') ch = '*';
    if(ch=='D') ch = '`';
    if(ch=='E') ch = 'X';
    if(ch=='F') ch = '&';
    if(ch=='G') ch = '2';
    if(ch=='H') ch = '_';
    if(ch=='I') ch = '^';
    if(ch=='J') ch = '-';
    if(ch=='K') ch = '>';
    if(ch=='L') ch = ']';
    if(ch=='M') ch = '"';
    if(ch=='N') ch = ':';
    if(ch=='O') ch = '1';
    if(ch=='P') ch = ';';
    if(ch=='Q') ch = '<';
    if(ch=='R') ch = '?';
    if(ch=='S') ch = '=';
    if(ch=='T') ch = '%';
    if(ch=='U') ch = '#';
    if(ch=='V') ch = '}';
    if(ch=='W') ch = '+';
    if(ch=='X') ch = '{';
    if(ch=='Y') ch = '/';
    if(ch=='Z') ch = '[';

    if(ch=='a') ch = 'F';
    if(ch=='b') ch = '0';
    if(ch=='c') ch = '6';
    if(ch=='d') ch = '3';
    if(ch=='e') ch = '9';
    if(ch=='f') ch = '5';
    if(ch=='g') ch = '4';
    if(ch=='h') ch = '7';
    if(ch=='i') ch = '8';
    if(ch=='j') ch = 'A';
    if(ch=='k') ch = 'D';
    if(ch=='l') ch = 'J';
    if(ch=='m') ch = 'Q';
    if(ch=='n') ch = 'E';
    if(ch=='o') ch = 'R';
    if(ch=='p') ch = 'Y';
    if(ch=='q') ch = 'Z';
    if(ch=='r') ch = 'K';
    if(ch=='s') ch = 'L';
    if(ch=='t') ch = 'V';
    if(ch=='u') ch = 'M';
    if(ch=='v') ch = 'U';
    if(ch=='w') ch = 'S';
    if(ch=='x') ch = 'B';
    if(ch=='y') ch = 'W';
    if(ch=='z') ch = 'C';
    fout << ch;
  }
  return 0;
}

Here is a little file I wrote just playing around, and it works.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close