| | |
replace characters in array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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: 392
Reputation:
Solved Threads: 72
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 beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets





