How to ignore certain letters and spaces in character arrays - c++

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++;
}
}

Related

How do I modify an input through multiple functions in C++?

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.

How do I print multiple inputs and outputs in C++?

I'm trying to solve a problem on a competitive programming book where the output only appears after entering in the last input. I seem to have gotten the logic down but I'm still confuse as to how to do the input/output portion.
Here is the code:
#include <bits/stdc++.h>
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::vector<int>soundex;
std::string word;
for(int i = 0; i < word.length(); i++)
{
if (word[i] == 'B'|| word[i] == 'F' || word[i] == 'P' || word[i] == 'V')
{
soundex.push_back(1);
}
if (word[i] == 'C' || word[i] == 'G' || word[i] == 'J' || word[i] == 'K' || word[i] == 'Q' || word[i] == 'S' || word[i] == 'X' || word[i] == 'Z')
{
soundex.push_back(2);
}
if (word[i] == 'D' || word[i] == 'T')
{
soundex.push_back(3);
}
if (word[i] == 'L')
{
soundex.push_back(4);
}
if (word[i] == 'M' || word[i] == 'N')
{
soundex.push_back(5);
}
if (word[i] == 'R')
{
soundex.push_back(6);
}
}
for (int j = 0; j < soundex.size(); j++)
{
if (soundex[j] == soundex[j+1])
{
soundex.erase(soundex.begin() + 1);
}
std::cout << soundex[j];
}
std::cout << "\n";
return 0;
}
It behaves like this:
Input:
KHAWN
Output:
25
Input:
PFISTER
Output:
1236
Input:
BOBBY
Output:
11
But I need it to behave like this, per the instructions of the problem:
Input:
KHAWN
PFISTER
BOBBY
Output:
25
1236
11
Use while(cin >> word){ ... your code ... } to read until EOF (End Of File) in case every line only contains a word (no spaces allowed). You can keep the output as it is.

delete all consonents from the string "Hello, have a good day"

I have seen many good code to do this problem. I am new to coding. My question is where my logic went wrong. I think that problem is with second string str1. I din't initialize it. even when I am printing element by element withing if , it is working. but it is not working, when I am trying to print whole string str1.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "Hello, have a good day", str1;
for (int i = 0, j =0; i < str.length(); ++i)
{
if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
if (str[i] == 'I' || str[i] == 'i' || str[i] == 'U' || str[i] == 'u' || str[i] == 'O' || str[i] == 'o' ||
str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' )
{
str1[j] = str[i];
//std::cout << str1[j] ;
j++;
}
else
{
str1[j] = str[i];
j++;
}
}
cout << str1 <<'\n';
}
output is just blank.
The first thing to do is to write a function that determines whether a character is a consonant:
bool is_not_consonant(char ch) {
static char consonants[] = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
return std::find(std::begin(consonants), std::end(consonants), ch) == std::end(consonants);
}
Then use that function as a predicate to std::copy_if:
std::string result;
std::string input = whatever;
std::copy_if(std::begin(input), std::end(input),
std::back_inserter(result),
is_not_consonant);
Explanation
The problem is that you don't need the else condition. All you need to do is check for a vowel, and print if found which is rightly covered in your if condition.
Code
Try this:
#include<string>
using namespace std;
int main()
{
string str = "Hello, have a good day", str1;
for (int i = 0; i < str.length(); ++i)
{
if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
if (str[i] == 'I' || str[i] == 'i' || str[i] == 'U' || str[i] == 'u' || str[i] == 'O' || str[i] == 'o' || str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' )
{
str1 += str[i];
}
}
cout << str1 <<'\n';
}

Probleem with substr in c++

I made binary calculator. It works, but only when numbers doesnt start with 0. if yes, it shows this:
How I should repair the program, please? (vysledek means result)
Part of code with substr:
while(vysledek[0] == '0')
{
vysledek = vysledek.substr(1, vysledek.length());
w++;
}
int aa;
aa = p-w;
for(o; o < aa; o++)
{
cout << vysledek[o];
}
Whole program:`
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
using namespace std;
int main()
{
cout << "Zadejte dve binarni cisla:" << endl;
string cislo1, cislo2;
cin >> cislo1 >> cislo2;
int c = 0;
for (c; c < cislo1.length(); c++)
{
if ((cislo1[c] == '0') || (cislo1[c] == '1'))
{
continue;
}
else
{
cout << "Nespravny vstup.";
return 0;
}
}
int d = 0;
for (d; d < cislo2.length(); d++)
{
if ((cislo2[d] == '0') || (cislo2[d] == '1'))
{
continue;
}
else
{
cout << "Nespravny vstup.";
return 0;
}
}
string stringS2 = "";
for(unsigned i = 0; i < cislo1.length(); i++)
{
stringS2 += cislo1[cislo1.length()-1-i];
}
cislo1 = stringS2;
string stringS3 = "";
for(unsigned i = 0; i < cislo2.length(); i++)
{
stringS3 += cislo2[cislo2.length()-1-i];
}
cislo2 = stringS3;
/*
cout << cislo1 << endl;
cout << cislo2 << endl;
cout << cislo1.length() << endl;
cout << cislo2.length() << endl;
*/
int p;
if (cislo1.length() > cislo2.length())
{
p = cislo1.length();
int f = cislo2.length();
for(f; f < cislo1.length(); f++)
{
cislo2[f] = '0';
}
}
else
{
p = cislo2.length();
int f = cislo1.length();
for(f; f < cislo2.length(); f++)
{
cislo1[f] = '0';
}
}
int e = 0;
//cout << p << endl;
string vysledek;
int t;
t = (p - 1);
cislo1[p];
cislo2[p];
for(e; e < p; e++)
{
/*nasledujici podminky jsou pro pripad, kdy cisla maji odlisny pocet znaku
if(cislo1[e] == NULL)
{
cislo1[e] == '0';
}
if(cislo2[e] == NULL)
{
cislo2[e] == '0';
}*/
if (cislo1[e] == '0' && cislo2[e] == '0')
{
if ((cislo1[e-1] == '1' && cislo2[e-1] == '1')|| ((cislo1[e-1] == '0' && cislo2[e-1] == '1')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '0')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '1')&& vysledek[t+1] == '1') )
{
vysledek[t] = '1';
}
else
{
vysledek[t] = '0';
}
//cout << vysledek[t] << endl;
t--;
}
else if ((cislo1[e] == '1' && cislo2[e] == '0') || (cislo1[e] == '0' && cislo2[e] == '1') )
{
if ((cislo1[e-1] == '1' && cislo2[e-1] == '1') || ((cislo1[e-1] == '0' && cislo2[e-1] == '1')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '0')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '1')&& vysledek[t+1] == '1'))
{
vysledek[t] = '0';
}
else
{
vysledek[t] = '1';
}
// cout << vysledek[t] << endl;
t--;
}
else if (cislo1[e] == '1' && cislo2[e] == '1')
{
if ((cislo1[e-1] == '1' && cislo2[e-1] == '1') || ((cislo1[e-1] == '0' && cislo2[e-1] == '1')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '0')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '1')&& vysledek[t+1] == '1'))
{
vysledek[t] = '1';
}
else
{
vysledek[t] = '0';
}
//cout << vysledek[t] << endl;
t--;
}
}
cout << "Soucet: ";
int u;
u = (p - 1);
if(((cislo1[u] == '0' && cislo2[u] == '1')&& vysledek[0] == '0') || ((cislo1[u] == '1' && cislo2[u] == '0')&& vysledek[0] == '0') || (cislo1[u] == '1' && cislo2[u] == '1') )
cout << '1';
int o = 0;
int z;
int w = 0;
while(vysledek[0] == '0')
{
vysledek = vysledek.substr(1, vysledek.length());
w++;
}
int aa;
aa = p-w;
for(o; o < aa; o++)
{
cout << vysledek[o];
}
return 0;
}
`
string vysledek; creates an empty string. It is never given any other size.
All following uses of vysledek[t] and vysledek.substr are all out of range.

Indefinite size of an array [duplicate]

This question already has answers here:
How to define member array size at runtime
(3 answers)
Closed 6 years ago.
I was trying to make a simple program that counts the number of vowels and consonants the user's name has. The size of the array is dependent on how many letters does the user's name has. I'm using queue to display the letters. When ever I run the program, the .Exe file crashes. So I tried to change the array size to a number instead of a variable and it worked. The problem is in using a variable for the size of the array, I think. Is there anyway to fix it? So that I can still use a variable for the size of my array. Here is my code
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int i;
char bin[i];
queue<char> name;
int v = 0, c = 0;
cout << "how many letters does your name have?:";
cin >> i;
cout << "Enter the letters of your name one by one:\n";
do {
for (int k = 0; k < i; k++) {
cout << " ";
cin >> bin[k];
name.push(bin[k]);
if (bin[k] == 'a' || bin[k] == 'A') {
v++;
}
if (bin[k] == 'e' || bin[k] == 'E') {
v++;
}
if (bin[k] == 'i' || bin[k] == 'I') {
v++;
}
if (bin[k] == 'o' || bin[k] == 'O') {
v++;
}
if (bin[k] == 'u' || bin[k] == 'U') {
v++;
}
if (bin[k] == 'b' || bin[k] == 'B') {
c++;
}
if (bin[k] == 'c' || bin[k] == 'C') {
c++;
}
if (bin[k] == 'i' || bin[k] == 'D') {
c++;
}
if (bin[k] == 'f' || bin[k] == 'F') {
c++;
}
if (bin[k] == 'g' || bin[k] == 'G') {
c++;
}
if (bin[k] == 'h' || bin[k] == 'H') {
c++;
}
if (bin[k] == 'j' || bin[k] == 'J') {
c++;
}
if (bin[k] == 'k' || bin[k] == 'K') {
v++;
}
if (bin[k] == 'l' || bin[k] == 'L') {
v++;
}
if (bin[k] == 'm' || bin[k] == 'M') {
c++;
}
if (bin[k] == 'n' || bin[k] == 'N') {
c++;
}
if (bin[k] == 'P' || bin[k] == 'p') {
c++;
}
if (bin[k] == 'q' || bin[k] == 'Q') {
c++;
}
if (bin[k] == 'r' || bin[k] == 'R') {
c++;
}
if (bin[k] == 's' || bin[k] == 'S') {
c++;
}
if (bin[k] == 't' || bin[k] == 'T') {
c++;
}
if (bin[k] == 'v' || bin[k] == 'V') {
c++;
}
if (bin[k] == 'w' || bin[k] == 'W') {
c++;
}
if (bin[k] == 'x' || bin[k] == 'X') {
c++;
}
if (bin[k] == 'y' || bin[k] == 'Y') {
c++;
}
if (bin[k] == 'z' || bin[k] == 'Z') {
c++;
}
}
} while (bin[100] != '1');
cout << "\n\nYour name is:\n";
for (queue<char> run = name; !run.empty(); run.pop()) {
cout << " " << run.front() << endl;
}
cout << "\nVowels= " << v;
cout << "\nConsonants= " << c << "\n";
cout << "\n\n\n\n";
system("PAUSE");
return 0;
}
Your code has undefined behavior. int i; creates an uninitialized int named i. Then you use the uninitialized garbage variable in char bin[i];. So now we have an array with undefined size.
At this point we can't go any further. Once undefined behavior is invoked we can no longer reason how the code should work. If you need an array and you are not going to know what the size is until run time then you should use a std::vector and push_back()