I wrote a program that lets the user input a character. If the character is a vowel, do nothing; if the character is a consonant, find the closest vowel in the alphabet. Is there any easy way to do this?
Here's what I have for now:
char input, output;
cin >> input;
if (input == 'b'){ output = 'a'; }
else if (input == 'c'){ output = 'a'; }
else if (input == 'd'){ output = 'e'; }
else if (input == 'f'){ output = 'e'; }
else if (input == 'g'){ output = 'e'; }
else if (input == 'h'){ output = 'i'; }
else if (input == 'j'){ output = 'i'; }
else if (input == 'k'){ output = 'i'; }
else if (input == 'l'){ output = 'i'; }
else if (input == 'm'){ output = 'o'; }
else if (input == 'n'){ output = 'o'; }
else if (input == 'p'){ output = 'o'; }
else if (input == 'q'){ output = 'o'; }
else if (input == 'r'){ output = 'o'; }
else if (input == 's'){ output = 'u'; }
else if (input == 't'){ output = 'u'; }
else if (input == 'v'){ output = 'u'; }
else if (input == 'w'){ output = 'u'; }
else if (input == 'x'){ output = 'u'; }
else if (input == 'y'){ output = 'u'; }
else if (input == 'z'){ output = 'u'; }
Is there a simpler way to do this?
You could also use an array:
const char nearestVowels[26] = "aaaeeeeiiiiioooooouuuuuyyy";
// abcdefghijklmnopqrstuvwxyz
Then you could simply do this:
output = nearestVowels[input - 'a'];
Looks like you want something like this:
if (input != 'a' && input != 'e' && input != 'i' && input != 'o' && input != 'u')
{
if ('a' <= input && input <= 'c') { output = 'a'; }
else if (input <= 'g') { output = 'e'; }
else if (input <= 'l') { output = 'i'; }
else if (input <= 'r') { output = 'o'; }
else if (input <= 'z') { output = 'u'; }
}
Live On Coliru
It can be done like this too:
if(input>='a'&&input<='z'){
if(input<='c')output='a';
else if(input<='g')output='e';
else if(input<='l')output='i';
else if(input<='r')output='o';
else if(input<='z')output='u';
}
The first if makes sure that no input other than a~z are fed into output.
There are very many ways to do this, perhaps the most concise (albeit ugly) is:
const char* p = strchr(input, "bacadefegehijikilimonopoqorosutuvuwuxuyuzu");
output = (p && *p) ? p[1] : input;
This works by seeking a char* to input in that string literal, then if found it returns the following character. It won't crash for any character code 0-255, but if you input a vowel it will find it in the string and return the following consonant - if that's a problem, it's probably better to use the following...
Have two arrays - one of consonants and another of vowels, and if you find input in the former get the vowel from the same index in the latter.
Yet another way is to use a std::map:
std::map<char, char> m { { 'b', 'a' }, {'c', 'a' } etc... };
if (auto i = m.find(input))
output = *i;
else
output = input;
switch is a bit more verbose than the above, but will likely give the fastest performance (but do measure if you care).
switch would be simpler:
switch (input)
{
case 'b':
case 'c':
output = 'a';
break;
case 'd':
case 'f':
case 'g':
output = 'e';
break;
...
etc.
}
This feels more generic:
Get the ASCII value of the input
Substract each vowels's ASCII value as its distance and store (vowel, distance) in a list
Sort the list
Get its first pair's key (the vowel)
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
bool compare(const pair<char, int>& a, const pair<char, int>& b)
{
return a.second < b.second;
}
int main()
{
char vowels[] = { 'a', 'e', 'i', 'o', 'u' };
char input;
//std::cin >> input;
input = 'r';
vector<pair<char, int>> distances;
for (char& vowel : vowels)
{
distances.push_back(make_pair(vowel, abs(input - vowel)));
}
sort(distances.begin(), distances.end(), compare);
pair<char, int> nearest = distances.front();
if (nearest.second > 0)
{
cout << nearest.first;
}
}
Here's one I made in Java, it's not assigned statically and supports lower and upper case letters.
public static char closestVowel(char character) {
int value = (int) character;
if(character == 'z' || character == 'Z')
return (char) (value - 25);
if(isVowel(character)) {
return character;
} else {
return closestVowel((char) (value - 1));
}
}
public static boolean isVowel(char character) {
return "aeiouAEIOU".indexOf(character) >= 0;
}
Related
Basically I have to encode a name into a Soundex Code. The helper functions I implemented do the following:
Discard all non-letter characters from the surname: dashes, spaces, apostrophes, and so on.
Encode each letter as a digit
Coalesce adjacent duplicate digits from the code (e.g. 222025 becomes 2025).
Replace the first digit of the code with the first letter of the original name, converting to uppercase.
Remove all zeros from the code.
Make the code exactly length 4 by padding with zeros or truncating the excess.
Excuse the implementation of the helper functions, I know they could be implemented better. But when I manually pass the output from one function to another I see that the result is what I want. It's only when I combine them all into one function that I see that the output I pass is as if I didn't modify the input I passed at all. I believe my issue might have to do with passing by reference but doing that for all my functions made no difference or gave an incorrect output.
#include <iostream>
#include <string>
string removeNonLetters(string s) {
string result = "";
for (int i = 0; i < s.length(); i++) {
if (isalpha(s[i])) {
result += s[i];
}
}
return result;
}
string encode(string name) {
std::transform(name.begin(), name.end(), name.begin(), ::toupper);
string encoded = "";
for (int i = 0; i < name.size(); ++i) {
if (name[i] == 'A' || name[i] == 'E' || name[i] == 'I' || name[i] == 'O' || name[i] == 'U' || name[i] == 'H' || name[i] == 'W' || name[i] == 'Y')
encoded += '0';
else if (name[i] == 'B' || name[i] == 'F' || name[i] == 'P' || name[i] == 'V')
encoded += '1';
else if (name[i] == 'C' || name[i] == 'G' || name[i] == 'J' || name[i] == 'K' || name[i] == 'Q' || name[i] == 'S' || name[i] == 'X' || name[i] == 'Z')
encoded += '2';
else if (name[i] == 'D' || name[i] == 'T')
encoded += '3';
else if (name[i] == 'L')
encoded += '4';
else if (name[i] == 'M' || name[i] == 'N')
encoded += '5';
else if (name[i] == 'R')
encoded += '6';
}
return encoded;
}
string removeDuplicate(string encoded) {
for (int i = 0; i < encoded.size(); ++i) {
if (encoded[i] == encoded[i+1])
encoded[i] = '\0';
}
return encoded;
}
string removeZeros(string digits) {
for (int i = 0; i < digits.size(); ++i) {
if (digits[i] == '0')
digits[i] = '\0';
}
return digits;
}
string padding(string output) {
int size = output.size();
if (size < 4) {
for (int i = size; i < 4; ++i)
output += '0';
}
else if (size > 4) {
for (int j = size; j > 3; --j)
output[j] = '\0';
}
return output;
}
/* TODO: Replace this comment with a descriptive function
* header comment.
*/
string soundex(string s) {
/* TODO: Fill in this function. */
string copy = s;
removeNonLetters(s);
encode(s);
removeDuplicate(s);
s[0]= copy[0];
removeZeros(s);
padding(s);
return s;
}
int main() {
string s = "Curie";
cout << soundex(s) << '\n';
// Output should be C600 but I keep getting "Curie."
}
Your functions return the adjusted strings, that's good. But your calling code doesn't use the returned values!
Something like this is what you want.
string soundex(string s) {
/* TODO: Fill in this function. */
string copy = s;
s = removeNonLetters(s);
s = encode(s);
s = removeDuplicate(s);
s[0] = copy[0];
s = removeZeros(s);
s = padding(s);
return s;
}
If you want to change the value of a variable you normally use =. I'm sure you know that but for some reason you forgot because functions are involved.
I am solving a question which states: to change every '?' with 'a' in a string if doesn't contain if won't form consecutive 'a' else substitute with 'b', eg. a?b will be abb and not aab because here 2 a's are consecutive.
My problem is for i = 3 my string should be over- written with 'b ' according to my code it is entering into the desired block but the string does n't gets written with b, but in all the other case where it should be witten with 'a' it get's written .Help me out with these.
You can refer the problem statement from here to for better understanding my problem :https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/practice-problems/algorithm/exploring-ruins/
#include <iostream>
using namespace std;
int main() {
string str;
cin >> str;
int n = str.size();
for(int i = 0; i < str.size(); i++) {
if(str[i] == '?') {
if(i == 0) {
if(str[i + 1] == 'a')
str[i] = 'b';
else
str[i] = 'a';
cout << "I am in if" << endl;
} else if(i == n - 1) {
if(str[i - 1] == 'a')
str[i] == 'b';
else
str[i] == 'a';
cout << "I am in if of else if " << endl;
} else {
if(str[i + 1] == 'a' || str[i - 1] == 'a') {
str[i] == 'b';
cout << "I am in if of else " << endl;
} else {
str[i] = 'a';
cout << "I am in else of else " << endl;
}
}
cout << str[i] << endl;
} else
continue;
}
cout << str << endl;
return 0;
}
Given string : ?ba??b
desired output : ababab
my output : aba?ab
It will be a lot easier for you if you would use functions to solve this problem.
bool check_neighbors_for_a(const string &str, size_t place) {
bool result = false;
if (place > 0) { // If there is a char before the current char
result = str[place - 1] == 'a'; // If the previous char is 'a' result become true
}
if (place < str.size() - 1) { // If there is a char after the current char
result = result || str[place + 1] == 'a'; // If the result has become true before this line, result will stay true. Else, result will be true if the next char is equal to 'a'.
// For example: b?a => result = (false || 'a' == 'a')
// For example: a?b => result = (true || 'b' == 'a')
// For example: a?a => result = (true || 'a' == 'a')
}
return result;
}
void replace_questions_by_a(string &str) {
for (size_t i = 0; i < str.size(); i++) {
if (str[i] == '?') {
if (check_neighbors_for_a(str, i)) { // If one of the neighbors is equal to 'a'
str[i] = 'b'; // Place 'b' instead of '?'
} else {
str[i] = 'a'; // Place 'a' instead of '?'
}
}
}
}
I'm working on a project for my computer science class, and for whatever reason, once my code gets to my cin.getline code it just ends and doesn't allow me to enter anything in and I don't know why. I think the problem has something to do with the "null" character at the end of a c-string, but I'm not quite sure as it was working fine until I added the "Determining Whether to CODE or DECODE section" and the if statement. This code isn't fully complete yet, I just need to fix whatever is causing this to move on though.
So essentially, it works without the whole section above it and the if statement. But once I add those, my cin.getline function doesn't work. However, if I do a cin >> line, it will work even with the code above it. I need the getline function because I will be writing it to a file, and I also need to grab those sexy spaces ' '.
#include <iostream>
#include <cstring>
#include <string>
#include <ctime>
#include <fstream>
using namespace std;
//Function Prototypes:
string fileAddress(string);
void swap(char &, char &);
string code(int, char [], char [], char []);
//Main Function:
int main()
{
//Alphabet Section:
//------------------------------------------------------------------------------------------------------------------
//Declaring Variables Relating to Alphabet:
int size = 29;
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ., ";
char sAlphabet[size];
int x;
int y;
//Copying Alphabet & Outputting Test Value:
strcpy(sAlphabet, alphabet);
//Scrambling Alphabet
unsigned seed = time(0);
srand(seed);
for(int i = 0; i < 10000; i++)
{
//Initializing Random Numbers to Swap:
x = rand()%29;
y = rand()%29;
//Swapping Values
swap(sAlphabet[x], sAlphabet[y]);
}
//Testing Scrambled Alphabet:
cout << sAlphabet << endl << endl;
//------------------------------------------------------------------------------------------------------------------
//Determining Whether to CODE or DECODE Section:
//------------------------------------------------------------------------------------------------------------------
//Response Variables:
string response;
//Gathering Choice From User:
cout << "If you wish to code a message, please type CODE." << endl;
cout << "If you wish to decode a message, please type DECODE.";
cin >> response;
//------------------------------------------------------------------------------------------------------------------
//Writing Coded Message to File Section:
//------------------------------------------------------------------------------------------------------------------
if(response == "CODE")
{
//Code Variables:
int length = 100;
char line[length];
char cline[length];
string codedLine;
//Gathering Line from User:
cout << "Please enter a message in all CAPS that you wish to code.";
cin.getline(line, length);
//Copying Line:
strcpy(cline, line);
//Gathering length of Line:
length = strlen(line);
codedLine = code(length, line, cline, sAlphabet);
cout << line << endl;
cout << codedLine<< endl;
}
else
cout << "You suck";
return 0;
}
//Creating Swap Function:
void swap(char &value1, char &value2)
{
char temp;
temp = value1;
value1 = value2;
value2 = temp;
}
string code(int length, char line[], char cline[], char sAlphabet[])
{
for(int i = 0; i < length; i++)
{
//Letter A:
if(line[i] == 'A')
{
cline[i] = sAlphabet[0];
}
//Letter B:
else if(line[i] == 'B')
{
cline[i] = sAlphabet[1];
}
//Letter C:
else if(line[i] == 'C')
{
cline[i] = sAlphabet[2];
}
//Letter D:
else if(line[i] == 'D')
{
cline[i] = sAlphabet[3];
}
//Letter E:
else if(line[i] == 'E')
{
cline[i] = sAlphabet[4];
}
//Letter F:
else if(line[i] == 'F')
{
cline[i] = sAlphabet[5];
}
//Letter G:
else if(line[i] == 'G')
{
cline[i] = sAlphabet[6];
}
//Letter H:
else if(line[i] == 'H')
{
cline[i] = sAlphabet[7];
}
//Letter I:
else if(line[i] == 'I')
{
cline[i] = sAlphabet[8];
}
//Letter J:
else if(line[i] == 'J')
{
cline[i] = sAlphabet[9];
}
//Letter K:
else if(line[i] == 'K')
{
cline[i] = sAlphabet[10];
}
//Letter L:
else if(line[i] == 'L')
{
cline[i] = sAlphabet[11];
}
//Letter M:
else if(line[i] == 'M')
{
cline[i] = sAlphabet[12];
}
//Letter N:
else if(line[i] == 'N')
{
cline[i] = sAlphabet[13];
}
//Letter O:
else if(line[i] == 'O')
{
cline[i] = sAlphabet[14];
}
//Letter P:
else if(line[i] == 'P')
{
cline[i] = sAlphabet[15];
}
//Letter Q:
else if(line[i] == 'Q')
{
cline[i] = sAlphabet[16];
}
//Letter R:
else if(line[i] == 'R')
{
cline[i] = sAlphabet[17];
}
//Letter S:
else if(line[i] == 'S')
{
cline[i] = sAlphabet[18];
}
//Letter T
else if(line[i] == 'T')
{
cline[i] = sAlphabet[19];
}
//Letter U:
else if(line[i] == 'U')
{
cline[i] = sAlphabet[20];
}
//Letter V:
else if(line[i] == 'V')
{
cline[i] = sAlphabet[21];
}
//Letter W:
else if(line[i] == 'W')
{
cline[i] = sAlphabet[22];
}
//Letter X:
else if(line[i] == 'X')
{
cline[i] = sAlphabet[23];
}
//Letter Y:
else if(line[i] == 'Y')
{
cline[i] = sAlphabet[24];
}
//Letter Z:
else if(line[i] == 'Z')
{
cline[i] = sAlphabet[25];
}
//Character Period:
else if(line[i] == '.')
{
cline[i] = sAlphabet[26];
}
//Character Comma:
else if(line[i] == ',')
{
cline[i] = sAlphabet[27];
}
//Character Space:
else if(line[i] == ' ')
{
cline[i] = sAlphabet[28];
}
else
cout << "Error" << endl;
}
return cline;
}
The output should be allowing myself to write in the line of code and see it back scrambled, which the scrambler works.
I fixed it using the ignore function for the \n character found in the thread that R Sahu posted.
I basically just added:
cin.ignore(std::numeric_limits::max(), '\n')
before my cin.getline.
i'm trying to rewrite this code in c++ which create a bitcoin mini private key
using System;
using System.Text;
using Org.BouncyCastle.Security;
public string CreateRandomMinikey(){
string keytotry = "S6c56bnXQiBjk9mqSYE7ykVQ7NzrQA";
char[] chars = keytotry.ToCharArray();
char[] charstest = (keytotry + "?").ToCharArray();
while (Util.ComputeSha256(utf8.GetBytes(charstest))[0] != 0) {// hash sha256 the key & check if the first character was '0'
// As long as key doesn't pass typo check, increment it.
for (int i = chars.Length - 1; i >= 0; i--) {
char c = chars[i];
if (c == '9') {
charstest[i] = chars[i] = 'A';
break;
} else if (c == 'H') {
charstest[i] = chars[i] = 'J';
break;
} else if (c == 'N') {
charstest[i] = chars[i] = 'P';
break;
} else if (c == 'Z') {
charstest[i] = chars[i] = 'a';
break;
} else if (c == 'k') {
charstest[i] = chars[i] = 'm';
break;
} else if (c == 'z') {
charstest[i] = chars[i] = '2';
// No break - let loop increment prior character.
} else {
charstest[i] = chars[i] = ++c;
break;
}
}
}
string result = new string(chars);//expect S6c56bnXQiBjk9mqSYE7ykVQ7NzrRy
return result;
}
Here my code when i transform into c++
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <cstring>
#include "sha256.h"
using namespace std;
int main()
{
string input = "S6c56bnXQiBjk9mqSYE7ykVQ7NzrQA";
string inputcharstest = input+"?";
char * chars = new char[input.size() + 1];
strcpy(chars, input.c_str());//convert input to char array
char * charstest = new char[inputcharstest.size() + 1];
strcpy(charstest, inputcharstest.c_str());// convert inputcharstest to char array
string output = sha256(inputcharstest); //hash sha256 inputcharstest to check typo
while (output[0] != '0') {
for (int i = strlen(chars) - 1; i >= 0; i--) {
char c = chars[i];
if (c == '9') {
charstest[i] = chars[i] = 'A';
break;
}
else if (c == 'H') {
charstest[i] = chars[i] = 'J';
break;
}
else if (c == 'N') {
charstest[i] = chars[i] = 'P';
break;
}
else if (c == 'Z') {
charstest[i] = chars[i] = 'a';
break;
}
else if (c == 'k') {
charstest[i] = chars[i] = 'm';
break;
}
else if (c == 'z') {
charstest[i] = chars[i] = '2';
}
else {
charstest[i] = chars[i] = ++c;
break;
}
}
}
string result = string(chars); //expect S6c56bnXQiBjk9mqSYE7ykVQ7NzrRy
cout << "input('" << input << "'):" << result << endl;
return 0;
}
This is the result i expected
The code was stuck & return blank when i run it on console application, i debug & find out it was stuck in while loop forever. May i ask if there're any problem with this & how do i fix it?
Here you are changing the content of the string every time:
char[] charstest = (keytotry + "?").ToCharArray();
while (Util.ComputeSha256(utf8.GetBytes(charstest))[0] != 0) {
charstest changes in every run through the loop, so the test in the while can terminate at some point. In the C++ code, however, you're not doing that:
string output = sha256(inputcharstest); //hash sha256 inputcharstest to check typo
while (output[0] != '0') {
Here, you're only doing the hash once before the loop, and then not again in the loop. The loop doesn't change output at all and neither does it break out of the while, so if output[0] != '0' is true the first time, it will always be true, thus the infinite loop.
Trying to make an else statement that get rid of all other letter and spaces then the ones i want. This function is to change user inputted letters into other letters
using namespace std;
void dna_to_rna(char rna[])
{
for (int i = 0; i < 100; i++)
{
if (rna[i] == 'a' || rna[i] == 'A')
rna[i] = 'U';
else if (rna[i] == 'c' || rna[i] == 'C')
rna[i] = 'G';
else if (rna[i] == 'g' || rna[i] == 'G')
rna[i] = 'C';
else if (rna[i] == 't' || rna[i] == 'T')
rna[i] = 'A';
}
What should the else statement look like in order to drop all other chars?
If the input parameter can be changed to std::string, then you can use one of the following implementation:
void dna_to_rna(std::string& rna)
{
auto it = rna.begin();
while (it != rna.end())
{
if (*it == 'a' || *it == 'A') *it = 'U';
else if (*it == 'c' || *it == 'C') *it = 'G';
else if (*it == 'g' || *it == 'G') *it = 'C';
else if (*it == 't' || *it == 'T') *it = 'A';
else
{
it = rna.erase(it);
continue; // it already "points" to the next element
}
++it;
}
}
std::string dna_to_rna(const std::string& dna)
{
std::string rna;
for (auto c : dna)
{
if (c == 'a' || c == 'A') rna += 'U';
else if (c == 'c' || c == 'C') rna += 'G';
else if (c == 'g' || c == 'G') rna += 'C';
else if (c == 't' || c == 'T') rna += 'A';
}
return rna;
}
Maybe like this:
using namespace std;
void dna_to_rna(char rna[])
{
string s = "";
for (int i = 0; i < 100; i++)
{
if (rna[i] == 'a' || rna[i] == 'A')
s += 'U';
else if (rna[i] == 'c' || rna[i] == 'C')
s += 'G';
else if (rna[i] == 'g' || rna[i] == 'G')
s += 'C';
else if (rna[i] == 't' || rna[i] == 'T')
s += 'A';
}
strcpy(rna, s.c_str());
}
The idea is simply to use a std::string as a temporary buffer. The string is empty to start with. Then you add the characters you want one-by-one. When done with the loop, copy the content of the std::string back to the rna-array.
To make you code much simpler, and easier to read:
using namespace std;
void dna_to_rna(char rna[]) {
int arrLength = sizeof(rna)/sizeof(rna[0]); // Get size of array
for (int i = 0; i < arrLength; i++){
if (toupper(rna[i]) == 'A'){
rna[i] = 'U';
}
else if (toupper(rna[i]) == 'C') {
rna[i] = 'G';
}
else if (toupper(rna[i]) == 'G'){
rna[i] = 'C';
}
else if (toupper(rna[i]) == 'T'){
rna[i] = 'A';
}
}
}
I created a second array and as long as the information that I was looking for met the criteria that was necessary I placed it into the second array making sure that the position that I was placing it in the array was always in the right spot by creating a second variable that would count the the proper position in the array then just cout array
using namespace std;
void dna_to_rna(char rna[])
{
int x = 0;
char newrna[100];
for (int i = 0; i < 100; i++)
{
if (rna[i] == 'a' || rna[i] == 'A')
{
newrna[x] = 'U';
x++;
}
else if (rna[i] == 'c' || rna[i] == 'C')
{
newrna[x] = 'G';
x++;
}
else if (rna[i] == 'g' || rna[i] == 'G')
{
newrna[x] = 'C';
x++;
}
else if (rna[i] == 't' || rna[i] == 'T')
{
newrna[x] = 'A';
x++;
}
}