int main()
{
string s;
//char a[5][8];
getline(cin,s);
int m= s.length();
char a[m][8];
//a == &city[0];
//s
char vowels[]={'a','e','i','o','u'};
for (int i = 0 ; i < m ; ++i)
{
if (s[i] !=*vowels)//, 'e' , 'i' , 'o' , 'u'))
{
s[i] = tolower(s[i]);
}
else //if (s[i]=vowels[5])
{
s[i]=toupper(s[i]);
}
cout << s[i] ;
}
return 0;
}
I am trying make all the vowels in uppercase and all consonents lowecase, but code is not working whats wrong with this?
Related
I am newbie and I have to check valid palindrome string.
Here is my code in c++
char to_lower(char ch){
char c = ch - 'A' + 'a';
return c;
}
bool isPalindrome(string s) {
int l = s.length();
string t = "";
for(int i=0;s[i]!='\0';i++){
if(((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z') || (s[i]>='0' && s[i]<='9')))
t.push_back(s[i]);
if(t[i]>='A' && t[i]<='Z')
t[i] = to_lower(t[i]);
}
int i=0;
int j=t.length()-1;
while(i<=j){
if(t[i]!=t[j])
return false;
else
i++;
j--;
}
return true;
}
It is showing stack buffer overflow. I am not understanding this error is showing. Please help me
I am trying to encrypt a string with white space and a question mark on it, for example, I am going to encrypt all the alphabet into numbers based on the order of the alphabet, but when I turn the string into numbers white space and question mark also gets enumerated how do I ignore that?
example)
int main() {
string alphabet;
int encrypted_code;
int first_int = 1, second_int = 2, third_int = 3;
string text = "hey what is your name?";
for (char ch = 'a'; ch <= 'z'; ch++) {
alphabet += ch;
}
for (int i = 0; i < text.length(); i++) {
encrypted_code = alphabet.find(text[i]);
if (i == 0 || i % 3 == 0) {
encrypted_code = encrypted_code + first_int;
}
cout << encrypted_code << " ";
}
}
Use an if statement to only work on letters:
for (int i = 0; i < text.length(); i++) {
if ('a' <= text[i] && text[i] <= 'z') {
// rest of your code goes here
}
}
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/
Given 2 strings, design a function that can check whether they are rotations to each other without making any changes on them ? The return value is boolean.
e.g ABCD, ABDC, they are not rotations. return false
ABCD, CDAB or DABC are rotations. return true.
My solution:
shift one of them to right or left one position and then compare them at each iteration.
If they are not equal at all iterations, return false. Otherwise, return true.
It is O(n). Are there other more efficient solutions ?
What if the contents of them cannot be changed ?
thanks
Concatenate the given string with the given string.
Search for the target string in the concatenated string.
Example:
Given = CDAB
After step 1, Concatenated = CDABCDAB
After step 2, Success CDABCDAB
^^^^
Rather than shifting one of them, it might be more efficient to use two index variables. Start one at 0 each time and the other at each of the possible positions (0 to N-1) and increment it mod N.
If you can't modify the strings, just take the first character of string1 and compare it to each character of string2. When you get a match, compare the second char of string1 to the next char of string2, and so on.
Pseudocode:
len = strlen(string1);
len2 = strlen(string2);
if( len != len2 )
printf("Nope.");
for( int i2=0; i2 < len; i2++ ) {
for( int i1=0; i1<len; i1++ ) {
if( string1[i1] != string2[(i2+i1)%len] )
break;
}
if( i1 == len ) {
print("Yup.");
break;
}
}
A simple one would be:
(s1+s1).find(s2) != string::npos && s1.size() == s2.size();
#include <iostream>
#include <cstring>
#include<string>
using namespace std;
void CompareString(string, string, int);
int ComputeStringLength(string str);
int main()
{
string str = ""; string str1 = ""; int len = 0, len1 = 0;
cout << "\nenter string ";
cin >> str;
cout << "\nenter string 2 to compare:- ";
cin >> str1;
len = ComputeStringLength(str);
len1 = ComputeStringLength(str1);
if (len == len1)
CompareString(str, str1, len);
else
cout << "rotation not possible";
getchar();
return 0;
}
int ComputeStringLength(string str)
{
int len = 0;
for (int i = 0; str[i] != '\0'; i++)
{
len++;
}
return len;
}
void CompareString(string str, string str1, int n)
{
int index = 0, flag = 0, curr_index = 0, count1 = 0, flagj = 0;
for (int i = 0; i<n; i++)
{
for (int j = flagj; j<n; j++)
{
if (str[i] == str1[j])
{
index = j;
flagj =j;
count1++;
flag++;
if (flag == 1)
{
curr_index = index;
}
break;
}
}
}
int temp = count1;
if (count1 != n)
{
if (curr_index>=0)
{
int k = 0;
for (int i = n - 1; i>n - curr_index - 1; i--)
{
if (str[i] == str1[k])
{
temp++;
k++;
}
}
}
if (temp == n)
{
cout << "\n\nstring is same after rotation";
}
else
{
cout << "\n\nstring is not same after rotation";
}
}
else
{
cout << "\n\nstring is same after rotation";
}
}
https://dsconceptuals.blogspot.in/2016/10/a-program-to-check-if-strings-are.html
I have the user enter a keyword which is broken up and put into a five by five array, then another function called "getletter" which fills up the rest of the spaces in the array in alphabetical order excluding those letters already included in the keyword. When I try to pass the values of the keyword to the getletter function it doesn't work.
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<limits>
using namespace std;
string getletter();
char type[81];
char filename[20];
char key [5];
char f[2] = "q";
char g[2] = "q";
char h[2] = "q";
char i[2] = "q";
char j[2] = "q";
char k[2] = "q";
char l[2] = "q";
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int main(){
string cipherarray[5][5]= {
{"a","f","k","p","v"},
{"b","g","l","r","w"},
{"c","h","m","s","x"},
{"d","i","n","t","y"},
{"e","j","o","u","z"}
};
cout<<"Enter the name of a file you want to create.\n";
cin>>filename;
cout<<"enter your codeword(codeword can have no repeating letters)\n";
cin>>key;
while (key[a] != '\0' ){
while(b <= 4){
cipherarray[b][c] = key[a];
if ( f == "q" ) {
cipherarray[b][c] = f;
}
if ( f != "q" && g == "q" )
{
cipherarray[b][c] = g;
}
if ( g != "q" && h == "q" )
{
cipherarray[b][c] = h;
}
if ( h != "q" && i == "q" )
{
cipherarray[b][c] = i;
}
if ( i != "q" && j == "q" )
{
cipherarray[b][c] = j;
}
if ( j != "q" && k == "q" )
{
cipherarray[b][c] = k;
}
if ( k != "q" && l == "q" )
{
cipherarray[b][c] = l;
}
a++;
b++;
if (key[a] == 0)
break;
}
if (key[a] != 0){
c++;
b = 0;
}
}
while ( c <= 4) {
while ( b <= 4) {
cipherarray[b][c] = getletter();
b++;
}
b = 0;
c++;
}
b = 0;
c = 0;
while ( c <= 4) {
while ( b <= 4) {
cout<<cipherarray[b][c]<<" ";
b++;
}
cout<<endl;
b = 0;
c++;
}
cout<<"now enter some text."<<endl<<"To end this program press Crtl-Z\n";
ofstream outFile;
outFile.open(filename);
outFile<<fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
cin.ignore(std::numeric_limits<int>::max(),'\n');
while(!cin.fail()){
cin.getline(type,81);
outFile<<type<<endl;
}
outFile.close();
}
string getletter() {
string letter;
string cipherarraytemplate[5][5]= {
{"a","f","k","p","v"},
{"b","g","l","r","w"},
{"c","h","m","s","x"},
{"d","i","n","t","y"},
{"e","j","o","u","z"}
};
if (cipherarraytemplate[d][e] == f || cipherarraytemplate[d][e] == g || cipherarraytemplate[d][e] == h || cipherarraytemplate[d][e] == i || cipherarraytemplate[d][e] == j ||
cipherarraytemplate[d][e] == k || cipherarraytemplate[d][e] == l){
d++;
}
else {
letter = cipherarraytemplate[d][e];
}
d++;
if (d == 5){
e++;
d = 0;
}
return letter;
}
I think the problem is in the comparisons
if ( j != "q" && k == "q" )
etc. The problem is that these variables are statically-typed as char [2], and so equality comparisons made on them will be comparing the addresses of the arrays and the pointers, not the contents of what's pointed at. To fix this, you might want to change this to read
if ( j != string("q") && k == string("q") )
which uses the string operator== function to do a true deep comparison.
It is not clear why you are using string rather than char for the array entries and the letter variables. E.g.
char f = 'q';
char getletter();
char cipherarray[5][5]= {
{'a','f','k','p','v'},
{'b','g','l','r','w'},
{'c','h','m','s','x'},
{'d','i','n','t','y'},
{'e','j','o','u','z'}
};
If you change all the string literals to single character literals and so on, you will avoid having to understand pointer semantics for this problem. Expressions such as:
if ( j != 'q' && k == 'q' )
will then just work.
I just set a variable that increases by one each time the loop runs through.