| | |
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: 398
Reputation:
Solved Threads: 73
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
Views: 2021 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays assignment beginner binary borland c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count data delete desktop dll encryption error file forms fstream function functions game givemetehcodez graph homework http iamthwee ifstream input int java lazy lib link linker list loop looping loops map math matrix memory newbie news number objects output pointer pointers problem program programming project python qt read recursion recursive reference return search sort sorting spoonfeeding string strings struct student studio system template templates text tree url variable vc++ vector video visual visualstudio win32 window windows winsock wordfrequency wxwidgets





