Using the C++ language, the function LetterChanges(str) takes the str parameter being passed and modifies it using the following algorithm.
Replace every letter in the string with the letter following it in the
alphabet (ie. c becomes d, z becomes a). Then capitalize every vowel
in this new string (a, e, i, o, u) and finally return this modified
string.
#include <iostream>
using namespace std;
string LetterChanges(string str) {
// code goes here
string str2 = "abcdefghijklmnopqrstuvwxyz";
int j;
for (int i = 0; str[i] != '\0'; i++) {
for (j = 0; str2[j] != '\0'; j++) {
if (str[i] == str2[j]) {
str[i] = str2[j + 1];
}
}
}
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == 'a') {
str[i] = 'A';
}
else if (str[i] == 'e') {
str[i] = 'E';
}
else if (str[i] == 'i') {
str[i] = 'I';
}
else if (str[i] == 'o') {
str[i] = 'O';
}
else if (str[i] == 'u') {
str[i] = 'U';
}
}
return str;
}
int main() {
// keep this function call here
cout << LetterChanges(gets(stdin));
return 0;
}
I am trying to run this code but it is not giving the desire output please help..
The Function
Let's start with your first loop:
for (int i = 0; str[i] != '\0'; i++) {
for (j = 0; str2[j] != '\0'; j++) {
if (str[i] == str2[j]) {
str[i] = str2[j + 1];
}
}
}
There are a couple things here. Firstly, we don't even need to deal with str2. Characters in C++ use ASCII encoding, meaning we can actually do something like str[i]++ to change an 'a' to a 'b' or an 'e' to an 'f', etc...
Also, I'd advise against using str[i] != '\0'. We're using the standard library strings instead of c-strings for a reason, so we might as well make our lives easier and use str.size(). Along these same lines, I'd suggest str.at(i) as opposed to str[i] as the former will do bounds checking for us.
Lastly, if you include cctype, then we can use the isalpha function to make sure we're only modifying alphabetic characters (no numbers or spaces, etc..).
Thus your first loop can become:
for (int i = 0; i < str.size(); i++) {
if(isalpha(str.at(i)){
if(str.at(i) == 'z') str.at(i) = 'a'; //special case
else str.at(i)++;
}
}
As far as your second loop, you don't even need it! We can actually incorporate everything straight into the first one. As long as we make sure to do the vowel modification after we've changed the individual letters.
A conversion from lowercase to uppercase can be done with some ASCII math as well. The difference between the lowercase and uppercase letters is 'A'-'a', so if we add that to any lowercase letter, it'll give us its uppercase version!
With all of this, we can modify your code to:
for (int i = 0; i < str.size(); i++) {
if(isalpha(str.at(i)){ //Make sure it's a letter!
if(str.at(i) == 'z') str.at(i) = 'a'; //special case
else str.at(i)++;
if(str.at(i) == 'a' | str.at(i) == 'e' | str.at(i) == 'i'
| str.at(i) == 'o' | str.at(i) == 'u') {
str.at(i) += 'A' - 'a';
}
}
Your Main
There's only one thing to fix here. Don't use gets for input. If you're looking for a single word, use the extraction operator, >>, or if you want a whole line, use getline.
string word, line;
getline(cin, line);
cin >> word;
cout << LetterChanges(line) << endl;
cout << LetterChanges(word) << endl;
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.
So i wanted to count all the characters in a string and categorized them in vowels, consonants, and specials character. For example;
Enter string: sentence example ..
Vowels: e(5) a(1)
Consonants: s(1) n(1) t(1) c(1) x(1) m(1) p(1) l(1)
Specials: blank space .(2)
Here's coding:
void characterType(string input)
{
int vowel = 0;
int consonant = 0;
int special = 0;
int n = input.size();
int freq[26];
memset(freq, 0, sizeof(freq));
for (int i = 0; i < n; i++)
{
freq[input[i] - 'a']++;
}
cout<<"Vowels: ";
for (int i = 0; i < n; i++)
{
char character = input[i];
if(isalpha(character))
{
character = tolower(character);
if (character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u')
{
cout<<input[i]<<freq[input[i] - 'a']<<" ";
}
}
}
cout<<endl;
cout<<"Consonants: ";
for (int i = 0; i < n; i++)
{
char character = input[i];
if(isalpha(character))
{
character = tolower(character);
if (character != 'a' || character != 'e' || character != 'i' || character != 'o' || character != 'u')
{
cout<<input[i]<<freq[input[i] - 'a']<<" ";
}
}
}
cout<<endl;
cout<<"Specials: ";
for (int i = 0; i < n; i++)
{
char character = input[i];
if(!isalpha(character))
{
if(character == ' ')
{
cout<<"[black space]"<<freq[input[i] - 'a']<<" ";
}
else
cout<<input[i]<<freq[input[i] - 'a']<<" ";
}
}
}
And heres what ive got so far:
How do i make it not repeat the same character and why does special characters is not counting?
Since you want to do this and it's not some type of assignment, here's how I would approach the problem, using modern C++ features:
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
int main() {
std::map<char, std::size_t> occurrance;
std::string input{"This is A long string with lots of characters in it. AAA$#!#$!^^! "};
auto is_vowel = [] (char c) -> bool
{
auto lc = std::tolower(c);
return
c == 'a' ||
c == 'e' ||
c == 'i' ||
c == 'o' ||
c == 'u';
};
auto is_special = [] (char c) -> bool
{
// use ascii table to find only "non-special characters"
if(c < '0') return true;
if(c > '9' && c < 'A') return true;
if(c > 'Z' && c < 'a') return true;
if(c > 'z') return true;
return false;
};
auto vowels = std::count_if(
input.begin(),
input.end(),
is_vowel);
auto special = std::count_if(
input.begin(),
input.end(),
is_special);
for(auto c : input)
{
occurrance[c] += 1;
}
std::cout << "Vowels: " << vowels << '\n';
std::cout << "Special: " << special << '\n';
for(auto [c, count] : occurrance)
{
std::cout << c << " -> " << count << '\n';
}
return 0;
}
https://ideone.com/YVaDiI
This does the job, although you can make the output prettier... and take the uppercase letters into account.
void printCharWithFreq(string str)
{
int v=0, c=0, s=0;
// size of the string 'str'
int n = str.size();
// 'freq[]' implemented as hash table
int freq[SIZE];
// initialize all elements of freq[] to 0
memset(freq, 0, sizeof(freq));
// accumulate freqeuncy of each character in 'str'
for (int i = 0; i < n; i++)
freq[str[i] - 'a']++;
// traverse 'str' from left to right
for (int i = 0; i < n; i++) {
// if frequency of character str[i] is not
// equal to 0
if (freq[str[i] - 'a'] != 0) {
// print the character along with its
// frequency
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U')
{ cout<<"Vowel: " << str[i] << freq[str[i] - 'a'] << " "<<endl; v+=freq[str[i] - 'a']; }
else if(str[i] == ' ' || str[i] == '.')
{ cout<<"Specials"<<endl; s++; }
else
{ cout<<"Consonant: " << str[i] << freq[str[i] - 'a'] << " "<<endl; c+=freq[str[i] - 'a']; }
// update frequency of str[i] to 0 so
// that the same character is not printed
// again
freq[str[i] - 'a'] = 0;
}
}
cout<<"Number of vowels: "<<v<<endl;
cout<<"Number of consonants: "<<c<<endl;
cout<<"Number of specials "<<s<<endl;
}
Then test it in the main function like
int main() {
string str = "some text . .";
printCharWithFreq(str);
return 0;
}
Basically, frequency is updated so it doesn't print the same character again, as it says in the comments. You also made a mistake in your code where you wrote:
character != 'a' || character != 'e' || character != 'i' || character != 'o' || character != 'u'
It is a consonant only if the input is different than ALL of the vowels, meaning you have to put &&, not ||.
As for the special characters, they can be counted with a simple counter.
The rest, I think, is clear from the comments in the code.
I adapted the code from the following source:
https://www.geeksforgeeks.org/print-characters-frequencies-order-occurrence/
The function I am using is
vector<string> tokenise(string s){
}
Firstly, I intend to split the string into substrings, in which case the string is always arithmetic expression (e.g. "100+5") and there could be some whitespaces.
"100+5" is needed to convert to "100", "+", "5"
After conversion, the substrings will be stored in a vector and return it. I am struggling with the fist step and using the subscript to loop over a string. The type of the value returned is char, so there is no way to put it in the vector.
You could just call the string's substring method, after figuring out the range of characters that are either digits, arithmetic characters, or unwanted.
You mentioned - The type of the value returned is char, so there is no way to put it in the vector.
You have some function that returns a character. You want to then insert the equivalent string into the vector.
Assuming your vector is defined as
std::vector<std::string> broken_strings;
So you can do it as follows.
char ch = ...; // Here comes the character that you get from the function.
std::string str(1, ch);
broken_strings.push_back(str);
Then you can return broken_strings.
Edit:
OP mentions that he wants to tokenize algebraic expressions.
So it will have to be done it a different way.
Following is a simple approach.
std::vector<std::string> broken;
std::string temp;
for ( int i = 0; i<s.length() ;i++){
char ch = s[i];
if (ch == ' ')
continue;
else if (ch >= '0' && ch <='9')
temp += ch;
else{
if (temp.length() != 0)
broken.push_back(temp);
temp = "";
temp += ch;
broken.push_back(temp);
temp = "";
}
}
if (temp.length() != 0)
broken.push_back(token);
return broken;
You can see the demo of the same here
Ideone
vector<string> tokenise(string s)
{
vector<string> v;
string number;
for(int i = 0; i < s.length(); ++i)
{
if((s[i] >= '0') && (s[i] <= '9'))
{
number += string(1, s[i]);
}
else if(s[i] == '.')
{
number += string(1, s[i]);
}
else if((s[i] == '+') || (s[i] == '-') || (s[i] == '*') || (s[i] == '/'))
{
if(number.size())
{
v.push_back(number);
number.clear();
}
v.push_back(string(1, c));
}
}
if(number.size())
{
v.push_back(number);
number.clear();
}
return v;
}
#include <iostream>
using namespace std;
char a[20];
int main()
{
cin >> a;
}
If I write for a="home", I want to take the vowels ("o" and "e") and replace them with capital letters ("O" and "E"). how do I do that?
EDIT:
Your answers where very helpful. I did something like this:
cin >> a;
for (int i = 0; a[i] != '\0' && i <= 20; i++)
{
if (a[i] == 'a')
a[i] = 'A';
if (a[i] == 'e')
a[i] = 'eE';
if (a[i] == 'i')
a[i] = 'iI';
if (a[i] == 'o')
a[i] = 'oO';
if (a[i] == 'u')
a[i] = 'uU';
}
I wanted to change for exemple "e" into "eE" but it doesn't work...
Write a function which will tell you whether something is a vowel or not. This can be as simple as looping through an array or using std::set.
Iterate through the characters and replace the vowels with the return value of toupper.
As a secondary note, you probably want to use std::string instead of char[].
Basically, you can do this:
#include <iostream>
using namespace std;
char a[20];
int main(){
cin >> a;
for (int i = 0; a[i] != '\0' && i < 20; i++){
if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i'|| a[i] == 'o'|| a[i] == 'u'){
a[i] = a[i] + 'A' - 'a';
}
}
cout << a;
}
The program iterates each character in the string, and compares it to all five vowels. If it finds it is a vowel, it turns it into uppercase.
The line
a[i] = a[i] + 'A' - 'a';
may seem hard to understand, but it isn't. Every character is actually an integer in a coding system. In most coding systems, the difference between a letter and its corresponding capital letter is a constant given by ('A' - 'a'). So, by adding ('A' - 'a') to any character, you effectively turn it into uppercase.
//inside the loop body
cin >> a;
while(a[i])
if(a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i]== 'o' || a[i]=='u')
{
a[i]=toupper(a[i]);
}
My input string is
\\?\bac#dos&ven_bb&prod_open-v&rev_5001#1&7f6ac24&0&353020304346333030363338#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
Required output is
bac\dos&ven_bb&prod_open-v&rev_5001\1&7f6ac24&0&353020304346333030363338_0
I have written a following code but is not working...need help is figuring out the problem.
Forgive my ignorance :) Also let me know if there is any better and efficient way to do it.
The rule for the output string is
In the second string i am removing all the "\" and "?" .And where is see the "#" i replace it with "\". and the second string is only till you see the charater "{" but does not include "#" at the end of it.
THanks
int main()
{
char s[] = "\\?\bac#dos&ven_bb&prod_open-v&rev_5001#1&7f6ac24&0&353020304346333030363338#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}";
char s1[] = {0};
printf("OUtput string is : ");
for(int i = 0; s[i] != '{'; i++)
{
if(s[i] != '\\' && s[i] != '?')
{
int j = 0;
if(s[i] == '#')
{
s1[j] = '\\';
continue;
}
s1[j] = s[i];
j++;
}
}
for(int i = 0; s1[i] != '\0'; i++)
{
cout<<s1[i];
}
getch();
}
I would suggest looking into using the std::string::replace() function. There is plenty of online documentation on this. Take a look at some of the other functions that std::string has to offer as they might be of use too. If you are using c++, the use of std::string is usually preferable to tinkering with char arrays and indices.
Note the fixed scope of j. In your version you were always assigning to s1[0].
for(int i = 0, j = 0; s[i] != '{'; i++)
{
if(s[i] != '\\' && s[i] != '?')
{
// int j = 0;
if(s[i] == '#')
{
s1[j] = '\\';
}
else
{
s1[j] = s[i];
}
j++;
}
}
The other thing is to allocate enough space for the new string. Since you haven't specified the size char s1[] = {0}; declares an array of size 1. You need to do something like:
char s1[sizeof(s)] = { 0 }; // the size of the old array, since we don't know how long the new one will be
But since you tagged the Q C++, take advantage of of dynamically resizable std::string.
std::string s = ".......";
std::string s1;
for(int i = 0; s[i] != '{'; i++)
{
if(s[i] != '\\' && s[i] != '?')
{
if(s[i] == '#')
s1 += '\\';
else
s1 += s[i];
}
}
Your s1 buffer needs to be increased, as it stands now there is no room for the new string.
E.g.
char* s1 = calloc(strlen(s)+1,sizeof(char)); // same size should be enough, free(s1) later
the calloc ensures that it is \0 terminated, in your code you forgotten to add the \0 so the printout act erratically.