Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing a character in a string

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
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
 
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,
Code:
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

All opinions are mine alone and do not necessarily reflect those of my employer.
 
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'..
 
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

All opinions are mine alone and do not necessarily reflect those of my employer.
 
Code:
// 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;
}
 
// Cvert1.cpp Converts text files

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

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

ifstream fin(argv[1]);
ofstream fout(argv[2]);
if(!fout) {
cout << &quot;Cannot open output file!\n&quot;;
return 1;
}
if(!fin) {
cout << &quot;Cannot open input file!\n&quot;;
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 = '&amp;';
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 = '&quot;';
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.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top