-
April 12th, 2018, 10:39 AM
#1
I'm a beginner . I kinda need help if one fine gentlemen or lady may help a fella out
Hello . I need some help . I'm trying to make a program that lets me add a text , then replaces every letter with its specific number alphabetically . Basically :
Example -> 5 28 1 13 16 12 5
This is what i've got so far and it doesn't look too good .
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char s[255];
int i;
cin.get (s,255); /// That's how i learned at school you read the text .
for(i = 1; i <= 31; i++) { /// 31 Letters are in the alphabet .
if (s[0] = 'a') /// I was about to make the same thing with b,c,d,etc.
cout << "1"; /// 1 because it's a .
}
return 0;
}
Last edited by 2kaud; April 12th, 2018 at 11:27 AM.
Reason: Added code tags
-
April 12th, 2018, 11:22 AM
#2
Re: I'm a beginner . I kinda need help if one fine gentlemen or lady may help a fella
When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.
Do you mean replace in the inputted text, or display the alphabet number - as your code in post #1 displays?
31 Letters are in the alphabet .
In the ASCII alphabet there are 26 letters uppercase and 26 lowercase.
Consider
Code:
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main()
{
string text;
cout << "Enter the text :";
getline(cin, text);
for (const auto& c : text)
if (isalpha(c))
cout << toupper(c) - 'A' + 1 << " ";
cout << endl;
}
Rather than use c s-style null terminated string (array of char), in c++ it is much better to use the string class. See http://www.cplusplus.com/reference/string/string/
cin.get (s,255); /// That's how i learned at school you read the text .
Then you were not taught properly! In c++ the good practice way to obtain a line of text from the console is to use getline().
isalpha() tests to see the if character is a letter.
To iterate over the chars in the entered text, use a range-based for loop. See http://en.cppreference.com/w/cpp/language/range-for
Treating lowercase and uppercase letters as the same (toupper() converts lower to upper) then the first char is 'A'. So subtracting 'A' from the char and adding 1 returns 1 for A, 2 for B etc upto 26 for Z.
Eg
Code:
Enter the text :Example
5 24 1 13 16 12 5
Last edited by 2kaud; April 12th, 2018 at 11:47 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 14th, 2018, 11:14 AM
#3
Re: I'm a beginner . I kinda need help if one fine gentlemen or lady may help a fella
This is the sort of thing I do with a lookup table like a map. You create a map with an index entry for everything you might be looking up and then a map entry for the correct response to the index.
There would be code something like,
Code:
#include <map>
#include <string>
// declare the namespace
using namespace std;
// declare the map
map<string, int> lookup_table;
// load map entries
lookup_table["a"] = 1;
lookup_table["b"] = 2;
lookup_table["c"] = 3;
// ... other entries
// declare a string to look up in your table
string looking_for;
// declare an int to hold the value returned from the map search
int num_version_of_letter = 0;
// declare a map iterator, this is a pointer to a location in the map
const map<string, int>::const_iterator letter_location;
// use find with the map to assign the location of the map index to your iterator
letter_location = lookup_table.find(looking_for);
// evaluate the value of the iterator to see if the index value you searched for
// is in the map, assign the map value if it is.
if(letter_location != lookup_table.end()) {
// if the letter (index) is in the map, assign the corresponding map value (second) to the int
num_version_of_letter = letter_location->second;
}
// print to the terminal if the index value is not found
else {
cout << "letter is not in the map" << endl;;
}
It would not be as efficient as the suggestion of 2kaud but it would be flexible in that you can easily add entries to the map, change the value that the map returns for a given index, handle instances where the index is not in the map, etc.
The above is not meant to be "working code" and is more along the lines of pseudo code that is intended to let you know what I mean. Let me know if you want a working version. A working version would probably have a separate function to load the map, to read the input, to lookup the input in the map, and to print the output.
LMHmedchem
Last edited by LMHmedchem; April 14th, 2018 at 02:23 PM.
-
April 15th, 2018, 01:20 AM
#4
Re: I'm a beginner . I kinda need help if one fine gentlemen or lady may help a fella
Originally Posted by LMHmedchem
It would not be as efficient as the suggestion of 2kaud
It could easily be made equally efficient (in the complexity sense) by simply using an std::unordered_map as lookup table (rather than an std::map)
but it would be flexible in that you can easily add entries to the map, change the value that the map returns for a given index, handle instances where the index is not in the map, etc.
Another advantage with the lookup approach is that it's independent of the underlying encoding of the characters.
-
April 15th, 2018, 04:53 AM
#5
Re: I'm a beginner . I kinda need help if one fine gentlemen or lady may help a fella
Note that the code in post #3 is c++98 code. Using current c++, one way could be
Code:
#include <map>
#include <string>
// declare the namespace
using namespace std;
// declare the map
const map<string, int> lookup_table {{"a", 1}, {"b", 2}, {"c", 3}}; // load map entries
// declare a string to look up in your table
string looking_for;
// declare an int to hold the value returned from the map search
int num_version_of_letter = 0;
// evaluate the value of the iterator to see if the index value you searched for
// is in the map, assign the map value if it is.
if (const auto letter_location = lookup_table.find(looking_for); letter_location != lookup_table.end())
// if the letter (index) is in the map, assign the corresponding map value (second) to the int
num_version_of_letter = letter_location->second;
// print to the terminal if the index value is not found
else
cout << "letter is not in the map" << endl;;
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 15th, 2018, 05:02 AM
#6
Re: I'm a beginner . I kinda need help if one fine gentlemen or lady may help a fella
Based upon the map method, consider for c++17
Code:
#include <unordered_map>
#include <string>
#include <optional>
using namespace std;
using Tfnd = size_t; // Type for associated key value
optional<Tfnd> find_str(const string& str)
{
const static unordered_map<string, Tfnd> lookup_table {{"a", 1}, {"b", 2}, {"c", 3}}; // load map as required
if (const auto letter_location = lookup_table.find(str); letter_location != lookup_table.end())
return letter_location->second;
else
return {};
}
int main()
{
string sfnd = "c";
if (const auto fnd = find_str(sfnd); fnd.has_value())
cout << "Value is " << fnd.value() << endl;
else
cout << "Not found\n";
}
This separates the find algorithm from its use. So if the algorithm for finding a value for a string changes, then only the function find-str() needs to be changed - and not its usage.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|