Every single time I run this program I get the number of vowels and consonants only
gets counted the number of spaces and any other special characters are not counted
I can't seem to figure out what is wrong in my code.
Does anybody have the solution to this?
void vowel_consonant(char *c)
{
int i,vcount,ccount,scount,ocount;
vcount=ccount=scount=ocount=0;
for (i=0;c[i]!='\0';i++)
{
if((c[i]=='a'||c[i]=='e'||c[i]=='i'||c[i]=='o'||c[i]=='u')||(c[i]=='A'||c[i]=='E'||c[i]=='I'||c[i]=='O'||c[i]=='U'))
vcount++;
else if((c[i]>=65 || c[i]<=90) && (c[i]>=97 || c[i]<=122))
ccount++;
else if(c[i]==' ')
scount++;
else
ocount++;
}
cout<<"Number of vowels:"<<vcount<<endl;
cout<<"Number of consonant:"<<ccount<<endl;
cout<<"Number of space:"<<scount<<endl;
cout<<"Number of extra:"<<ocount<<endl;
}
Related
In this question we have to print the all minimum size string to reach the target word. When i solve this question on GFG, it runs fine but not on LeetCode.
Here is my code:
class Solution {
public:
vector<vector<string>> findSequences(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> st(wordList.begin(), wordList.end());
queue<vector<string>> p;
p.push({beginWord});
vector<string> usedOnLevel;
usedOnLevel.push_back(beginWord);
int level = 0;
vector<vector<string>> ans;
while (!p.empty()) {
vector<string> vec = p.front();
p.pop();
if (vec.size() > level) {
level++;
for (auto it : usedOnLevel) {
st.erase(it);
}
}
string word = vec.back();
if (word == endWord) {
if (ans.size() == 0) {
ans.push_back(vec);
} else if (ans.size() > 0 && ans[0].size() == vec.size()) {
ans.push_back(vec);
}
}
for (int i = 0; i < word.length(); i++) {
char original = word[i];
for (char ch = 'a'; ch <= 'z'; ch++) {
word[i] = ch;
if (st.find(word) != st.end()) {
usedOnLevel.push_back(word);
vec.push_back(word);
p.push(vec);
vec.pop_back();
}
}
word[i] = original;
}
}
return ans;
}
};
The difference is that leetcode throws bigger problems at you, and so correct code with poor performance is going to break. And your code has poor performance.
Why? Well, for a start, for each word you find a path to, for each possible substitution, you're looking through all words to find yours. So suppose I start with all of the 5 letter words in the official Scrabble dictionary. There are about 9000 of those. For each word you find you're going to come up with 26*5 = 130 possible new words, then search the entire 9000 word list for that for 1_170_000_000 word comparisons, mostly to find nothing. Your algorithm wanted to do more than just that, but it has already timed out.
How could you make that faster? Here is one idea. Create a data structure to answer the following question:
by position of the deleted letter:
by the resulting string:
list of words that matched
For the entire Scrabble dictionary this data structure only has around 45_000 entries. And makes it easy to find all words next to a given word in the word ladder.
OK, great! Is that enough? Well...probably not. You're starting from startWord and finding all chains of words you can find from there. Most of which are going nowhere near endWord and represent wasted work. If the minimum length chain is fairly long, this can easily be an exponential amount of wasted effort. How can we avoid it?
The answer is to do a breadth-first search from endWord to find out how far away each word is from endWord. In this search we can also record for each word which words moved you closer. Again, even for all of the Scrabble dictionary, this data structure will be of manageable size. And you can break it off as soon as you've found how to get to startWord.
But now with this pre-processing, it is easy to start with startWord and recursively find all solutions. Because all of the work you'll be doing is enumerating paths that you already know will work.
Below are my codes for Leetcode 20. (Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.)
When the input is "(])" I still got true. Can anyone let me know what is wrong with my code? Thanks!
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(char c: s.toCharArray()){
if(c == '(' || c == '[' || c == '{'){
stack.push(c);
}else{
if(stack.empty()){
return false;
}
if(c == ')' && stack.peek() == '('){
stack.pop();
}
if(c == ']' && stack.peek() == '['){
stack.pop();
}
if(c == '}' && stack.peek() == '{'){
stack.pop();
}
}
}return stack.empty();
}
}
On the second iteration of the for loop you have char ], it doesn't match the first conditional so it goes on to the else block. None of the other if statements match, therefor it doesn't know what to do and just starts on the 3rd iteration of the loop, where it sees ) and also sees ( on peek so returns empty. This is where the issue lies. You'll need to add an additional else inside your else block to catch anything that doest match the 4 ifs.
In order to fix this particular test, add a check for the ] character only. if you see that character and you havent seen any [s then return false
Hopefully that helps, if not, let me know and I can try to clarify more.
Been at this for hours and hours. The indexOf() function never returns > -1 even when I am seeing the characters print in the serial monitor, and the length of the string is increasing as characters come in.
String command;
void loop() {
while ( bleuart.available() ) {
char ch;
ch = bleuart.read();
command += ch;
Serial.print(ch);
}
Serial.println(command.length());
if(command.indexOf("\n") > -1 ) {
Serial.println("command:");
Serial.println(command);
}
}
#darc is right, but there really shouldn't be a actual \n in the string if it comes from a ble module, which i assume. This should be a new line character (DEC 10) which \n should be the same as.
I see 3 deferent problems in your code.
Your command variable is never cleared, so it will just grow until
you run out of memory. When you are done with the command, call
command = "";
When you constructing your command string use command.concat(ch);instead of command += ch;
You should change if(command.indexOf("\n") > -1 ) to if(command.indexOf("\n") != -1 )
See if that helps.
I am trying to count two binary numbers from string. The maximum number of counting digits have to be 253. Short numbers works, but when I add there some longer numbers, the output is wrong. The example of bad result is "10100101010000111111" with "000011010110000101100010010011101010001101011100000000111000000000001000100101101111101000111001000101011010010111000110".
#include <iostream>
#include <stdlib.h>
using namespace std;
bool isBinary(string b1,string b2);
int main()
{
string b1,b2;
long binary1,binary2;
int i = 0, remainder = 0, sum[254];
cout<<"Get two binary numbers:"<<endl;
cin>>b1>>b2;
binary1=atol(b1.c_str());
binary2=atol(b2.c_str());
if(isBinary(b1,b2)==true){
while (binary1 != 0 || binary2 != 0){
sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0){
sum[i++] = remainder;
}
--i;
cout<<"Result: ";
while (i >= 0){
cout<<sum[i--];
}
cout<<endl;
}else cout<<"Wrong input"<<endl;
return 0;
}
bool isBinary(string b1,string b2){
bool rozhodnuti1,rozhodnuti2;
for (int i = 0; i < b1.length();i++) {
if (b1[i]!='0' && b1[i]!='1') {
rozhodnuti1=false;
break;
}else rozhodnuti1=true;
}
for (int k = 0; k < b2.length();k++) {
if (b2[k]!='0' && b2[k]!='1') {
rozhodnuti2=false;
break;
}else rozhodnuti2=true;
}
if(rozhodnuti1==false || rozhodnuti2==false){ return false;}
else{ return true;}
}
One of the problems might be here: sum[i++]
This expression, as it is, first returns the value of i and then increases it by one.
Did you do it on purporse?
Change it to ++i.
It'd help if you could also post the "bad" output, so that we can try to move backward through the code starting from it.
EDIT 2015-11-7_17:10
Just to be sure everything was correct, I've added a cout to check what binary1 and binary2 contain after you assing them the result of the atol function: they contain the integer numbers 547284487 and 18333230, which obviously dont represent the correct binary-to-integer transposition of the two 01 strings you presented in your post.
Probably they somehow exceed the capacity of atol.
Also, the result of your "math" operations bring to an even stranger result, which is 6011111101, which obviously doesnt make any sense.
What do you mean, exactly, when you say you want to count these two numbers? Maybe you want to make a sum? I guess that's it.
But then, again, what you got there is two signed integer numbers and not two binaries, which means those %10 and %2 operations are (probably) misused.
EDIT 2015-11-07_17:20
I've tried to use your program with small binary strings and it actually works; with small binary strings.
It's a fact(?), at this point, that atol cant handle numerical strings that long.
My suggestion: use char arrays instead of strings and replace 0 and 1 characters with numerical values (if (bin1[i]){bin1[i]=1;}else{bin1[i]=0}) with which you'll be able to perform all the math operations you want (you've already written a working sum function, after all).
Once done with the math, you can just convert the char array back to actual characters for 0 and 1 and cout it on the screen.
EDIT 2015-11-07_17:30
Tested atol on my own: it correctly converts only strings that are up to 10 characters long.
Anything beyond the 10th character makes the function go crazy.
I'm new to programming and I'm using Dart for a “introduction to programming“ course.
I'd like to write a code to validate if a text has only letter and space. I've found this for validate spaces.
function validate() {
var field = document.getElementById("myField");
if (field.value.replace(/ /g, "").length == 0) {
alert("Please enter some data (not spaces!)");
}
}
Thanks in advance, also I would need to write a string variable with sentences of different lengths and a function that finds the longest sentence. Any pointers?
[^a-z ] matches anything that is not a lowercase letter or space. caseSensitive: false makes it not match upper case either.
RegExp exp = new RegExp(r"[^a-z ]", caseSensitive: false);
print(exp.allMatches("this is valid").length == 0);
print(exp.allMatches("ThIs Is VaLiD").length == 0);
print(exp.allMatches("Th1s 1s NOT val1d").length == 0);
You can use exp.allMatches("string").length to find the number of characters that are not alpha or whitespace. so you can use:
if (exp.allMatches(field.value).length > 0) {
alert("Please enter some data (not spaces!)");
}