| | |
replace characters in array
![]() |
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Nov 2008
Posts: 377
Reputation:
Solved Threads: 69
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
Some care will also be needed on the issue of capitals and non- alphabet input, etc.
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
c++ Syntax (Toggle Plain Text)
outputLetter = newTarget[inputLetter-'a'];
Some care will also be needed on the issue of capitals and non- alphabet input, etc.
![]() |
Similar Threads
- String manipulation as array of char (C++)
- Help me figure this question (C)
- Replacing 2 characters with with preg_replace (PHP)
- Coverting a string to a byte array (Visual Basic 4 / 5 / 6)
- Deleting Characters Function (C)
- program dying?? any ideas why? (C++)
- miserable fgets and a 2d array (C)
- Help Me Please (C++)
- matching regular expressions (Java)
Other Threads in the C++ Forum
- Previous Thread: Remove a column in dataGridView
- Next Thread: How to Sum a Pascal Triangle
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets





