replace characters in array

Please support our C++ advertiser: Download Intel® Parallel Studio Eval
Reply

Join Date: Dec 2008
Posts: 3
Reputation: azagorath is an unknown quantity at this point 
Solved Threads: 0
azagorath azagorath is offline Offline
Newbie Poster

replace characters in array

 
0
  #1
Dec 3rd, 2008
hey guys , i am trying to write this program , the program has 3 arrays
char source[]="abcdefghijklmnopqrstuvwxyz";
char target[]="jfghdfsdyncdbdfklhdslasudfds";
char line[26];
now i have to ask the user to enter a line
cin.getline(line,26)
what i want to do is to check the characters in the user input(line) if it exist in the source array.and then replace each characters with the corresponding one from target array.for example if user enter the word "dad", the program have to check for d,a,d in the source and replace them with the corresponding one from target in this case the output will be djd. any ideas i would really appreciated.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 90
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 20
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: replace characters in array

 
0
  #2
Dec 3rd, 2008
Simple, for each character in the line array, search through the source array for the character, then use the same index to replace the characters with that of the target array...You'll most likely want to use two for-loops.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: geekru2 is an unknown quantity at this point 
Solved Threads: 0
geekru2 geekru2 is offline Offline
Newbie Poster

Re: replace characters in array

 
0
  #3
Dec 4th, 2008
two for loops will not be necessary, a single one would do,

for each character in the input string,
substitute with target[char - 'a']

since u are going to have one to one correspondence, the requirement of source array is nill
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: azagorath is an unknown quantity at this point 
Solved Threads: 0
azagorath azagorath is offline Offline
Newbie Poster

Re: replace characters in array

 
0
  #4
Dec 4th, 2008
i was trying to use strcmp to compare string char between line and source but i still stuck in the replacment part
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 398
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 73
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: replace characters in array

 
0
  #5
Dec 4th, 2008
Assuming that you have a source array that isn't abc... then you want to do it with one + one loop. I don't know why you read only 26 characters from the keyboard line. What if I have a long sentence ...
If you have a loop in a loop say for an input of 500 charaters and 26 letters, then you have 500*26/2 as the number of operations (assuming you find the letter you are after in 13 tries on average).
That is a huge overhead

So you create a mapping of a->? and b->? in another array and then
pass your input line.

You will need to get the correct look up for the array carefully done
  1.  
  2. outputLetter = newTarget[inputLetter-'a'];

Some care will also be needed on the issue of capitals and non- alphabet input, etc.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: azagorath is an unknown quantity at this point 
Solved Threads: 0
azagorath azagorath is offline Offline
Newbie Poster

Re: replace characters in array

 
0
  #6
Dec 4th, 2008
the reason i made it 26 letters is just for testing the code , once i get my code right i can make the input bigger.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2021 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC