var input = Console.ReadLine();
int letterCount = 0;
int wordCount = 0;
int specialChar = 0;
for (int i = 0; i <= input.Length-1; i++)
{
if ((input[i] >= 'a'&& input[i] <= 'z' )|| (input[i] >= 'A' && input[i] <= 'Z'))
{
letterCount++;
}
else if (input[i] >='0'&&input[i]<='9')
{
wordCount++;
}
else
{
specialChar++;
}
}
(input[i] >= 'a'&& input[i] <= 'z' )|| (input[i] >= 'A' && input[i] <= 'Z') this is whats bugging me. Shouldn't I have to write input.Length[i] how does the program know that I am still using the input length ?
Your limitations defined in this loop, "i" will go from 0 to the length-1.
for (int i = 0; i <= input.Length-1; i++)
each time you enter the loop you will take input[0], input[1] and so on, that way you will iterate over all the input
Also Input.Length[i] in not valid.
Input.Length will return length
Input[I] will return the value in place "i" in the input.
Related
Heyo,
I' am writing a game for my uni project, and can't get a piece of my code to detect if they completed the game correctly. The game is https://brainbashers.com/abcview.asp (https://www.brainbashers.com/showabcview.asp?date=1206&which=3). I am checking if there are only 1 A, only 1B, only 1 C in a line/column. Checked it and it does not detect the letter written. Does Anyone have some ideas?
int a = 0, b = 0, c = 0;
for (int i = 3; i < 9; i++)
{
for (int j = 3; j < 9; j++)
{
if (gamefield[i][j] == 'A')
a++;
else if (gamefield[i][j] == 'B')
b++;
else if (gamefield[i][j] == 'C')
c++;
}
if (a == 1 || b == 1 || c == 1)
{
}
else {return false; }
a = 0; b = 0; c = 0;
}
a = 0; b = 0; c = 0;
for (int i = 3; i < 9; i++)
{
for (int j = 3; j < 9; j++)
{
if (gamefield[j][i] == 'A')
a++;
else if (gamefield[j][i] == 'B')
b++;
else if (gamefield[j][i] == 'C')
c++;
}
if (a == 1 || b == 1 || c == 1)
{
}
else {
return false;
}
a = 0; b = 0; c = 0;
}```
can you try to use the ASCII values of the letter instead of the letter itself,
for example to verify if a letter equals 'A' you would write :
if (gamefield[j][i] == 65)
when I learning the KMP algorithm. I found out if I write the size() function as a condition in the while loop I will get the wrong result.
I found if I set size() function as condition. It can't enter the while loop in second time.
For example, if haystack="hello" and needle="ll". The correct answer is 2. But in this code. I get -1 as result.Because I found when I enter while loop in first time. Value of j become '-1'. But value of i still less than haystack.size() and same time value of j still less than needle.size().
why can't I write the code in this form.
This is my code:
class Solution {
public:
int strStr(string haystack, string needle) {
if (needle.size() > haystack.size())
return -1;
if (haystack == "" || needle == "")
return 0;
std::vector<int> next(needle.size(), 0);
getNext(next, needle);
int i = 0, j = 0;
while (i < haystack.size() && j < needle.size()) // it is question location
{
if (j == -1 || haystack[i] == needle[j])
{
i++;
j++;
}
else
j = next[j];
}
if (j == needle.size())
return i - j;
else
return -1;
}
void getNext(std::vector<int>& next, string needle)
{
next[0] = -1;
int i = 0, j = -1;
while (i < needle.size()-1)
{
if (j == -1 || needle[i] == needle[j])
{
++i;
++j;
next[i] = j;
}
else
j = next[j];
}
}
};
If I write this code. It's correct
class Solution {
public:
int strStr(string haystack, string needle) {
if (needle.size() > haystack.size())
return -1;
if (haystack == "" || needle == "")
return 0;
std::vector<int> next(needle.size(), 0);
getNext(next, needle);
int i = 0, j = 0;
int p = haystack.size();//look at this
int q = needle.size(); //look at this
while (i < p && j < q)
{
if (j == -1 || haystack[i] == needle[j])
{
i++;
j++;
}
else
j = next[j];
}
if (j == needle.size())
return i - j;
else
return -1;
}
void getNext(std::vector<int>& next, string needle)
{
next[0] = -1;
int i = 0, j = -1;
while (i < needle.size()-1)
{
if (j == -1 || needle[i] == needle[j])
{
++i;
++j;
next[i] = j;
}
else
j = next[j];
}
}
};
One thing that stands out to me is your comparison of j to -1, telling me you expect it at some point to get negative (presumably when there is no next value.)
Assuming j actually is negative, your loop is expected to terminate.
But it won't.
There's a nasty bug of comparing signed and unsigned values, which has surprising results.
Note, (-1 < 5u) is false (really!!!), because the negative int is promoted to unsigned, and -1 represented as unsigned is max unsigned. So the loop doesn't terminate when it should.
int i = 0, j = 0;
while (i < haystack.size() && j < needle.size()) // BUG: signed/unsigned mixture
{
if (j == -1 || haystack[i] == needle[j])
{
i++;
j++;
}
else
j = next[j];
}
Contrast that to when you store the size in an int, then you are doing integer comparisons and the loop terminates properly:
int p = haystack.size();
int q = needle.size();
while (i < p && j < q) // *** int to int comparison == good
{
Hi guys I've run into a problem,for some reason a blank string is being printed or you could also say nothing is being printed when I try to print out the string,this only occurs when I include a capital letter in the string such as acB if I type acb it sorts and prints them with no problems,I added a continue statement in to the for loop because I thought this would skip the rest of the code and go to the next iteration if that block of code got executed but to no avail anyway here is my code.
void order(char *str,int size){
bool sorted = false;
while(!sorted){
sorted = true;
for(int i = 0; i < size-1; i++){
if(str[i] >= 'A' && str[i] <= 'Z'){
if((str[i+1])-32 < str[i]){
char temp2 = str[i];
str[i] = str[i+1];
str[i+1] = temp2;
sorted = false;
continue;
}
}
if(str[i+1] < str[i]){
char temp = str[i];
str[i] = str[i+1];
str[i+1] = temp;
sorted = false;
}
}
}
}
int main()
{
char str[] = "aCb";
int size = sizeof(str) / sizeof(char);
order(str,size-1);
cout << str << endl;
}
void order(char *str,int size){
bool sorted = false;
while(!sorted){
sorted = true;
for(int i = 0; i < size - 1; i++){
if(str[i+1] >= 'A' && str[i+1] <= 'Z' && str[i] > 'Z'){
if(str[i+1] < str[i] - 32){
char temp2 = str[i];
str[i] = str[i+1];
str[i+1] = temp2;
sorted = false;
continue;
}
}
else{
if(str[i] >= 'A' && str[i] <= 'Z' && str[i + 1] >= 'Z'){
if(str[i+1] -32 < str[i]){
char temp2 = str[i];
str[i] = str[i+1];
str[i+1] = temp2;
sorted = false;
continue;
}
}
if(str[i+1] < str[i]){
char temp2 = str[i];
str[i] = str[i+1];
str[i+1] = temp2;
sorted = false;
continue;
}
}
}
}
}
int main()
{
char str[] = "aCB";
int size = sizeof(str) / sizeof(char);
order(str,size-1);
cout << str << endl;
}
First IF: Checks if the first letter is lowercase and the second letter is uppercase.
Second IF: Checks if the first letter is uppercase and the second letter is lowercase.
Third IF: then both letters are uppercase or lowercase.
what I want to do is :
Input a sentence from the user. Use full stop, space and comma as word separators. Each word should be stored in a 2D array whose columns vary in size and each row stores one word as a NULL terminated string.
For example, if the user inputs:
Hello how are you?
It should be stored as:
H E l l o NULL
h o w NULL
a r e NULL
y o u ? NULL
so whenever I try to run my code either this error appears
Exception thrown at 0x00832605 in Project109.exe: 0xC0000005: Access violation writing location 0xFDFDFE03.
or the program stops working.major problem is in
ptr[i][j] = str1[j];
`
char str1[20];
cin.get(str1, 20);
int len, sum = 0;
len = strlen(str1);
int i = 0;
while (str1[i] != '\0')
{
if (str1[i] == ' ' || str1[i] == '.' || str1[i] == ',' || str1[i] == '?' || str1[i] == ';')
{
sum = sum + 1;
}
i++;
}
char **ptr;
ptr = new char*[sum];
for (int i = 0; i < sum; i++)
{
ptr[i] = new char[20];
}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < 20; j++)
{
ptr[i][j] = '\0';
}}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < 20; j++)
{
if (str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '?' || str1[j] == ';')
{
i++;
}
else
{
ptr[i][j] = str1[j];
}
}
}
for (int i = 0; i < sum; i++)
{
int j = 0;
while (ptr[i][j] != '\0')
{
cout << ptr[i][j];
j++;
}
}
for (int i = 0; i < sum; i++)
{
delete[] ptr[i];
}
delete[] ptr;
system("pause");
return 0;}
`
You are indexing out of range, and hitting memory with fence bytes containing 0xFD.
Consider the loop here
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < 20; j++)
{
if (str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '?' || str1[j] == ';')
{
i++;
}
else
{
ptr[i][j] = str1[j];
}
}
}
If i is already at (or near) it's maximum value, in the inner loop you might increment it one or more times before reaching ptr[i][j] = str1[j];. At that time i might be way more than sum.
better solution but output is not that as required :
{ char str1[20];
cin.get(str1, 20);
int len, sum = 0;
len = strlen(str1);
int i = 0;
while (str1[i] != '\0')
{
if (str1[i] == ' ' || str1[i] == '.' || str1[i] == ',' || str1[i] == '?' || str1[i] == ';')
{
sum = sum + 1;
}
i++;
}
char **ptr;
ptr = new char*[sum];
for (int i = 0; i < sum; i++)
{
ptr[i] = new char[len];
}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < len; j++)
{
if (str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '?' || str1[j] == ';')
{
ptr[i][j] = str1[j];
i++;
}
else
{
ptr[i][j] = str1[j];
}
}
}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < len; j++)
{
cout << ptr[i][j];
}
cout << endl;
}
for (int i = 0; i < sum; i++)
{
delete[] ptr[i];
}
delete[] ptr;
system("pause");
return 0;}
I'm having some issues in creating a function that encrypts a word using a rotation number the user inputs. Here is what I have so far:
string encryptWord(string word, int num)
{
string newWord;
newWord = word;
for(int i = 0; i < word.length(); i++)
{
newWord[i] = tolower(word[i]);
if((word[i] >= 'a') && (word[i] <= 'z'))
{
newWord[i] = word[i] + (num % 26);
if(newWord[i] > 'z')
newWord[i] = newWord[i] - 26;
}
}
return newWord;
}
now in my main when I test it out with
cout << encryptWord("xyz", 6);
the output I get is: de
Similarly, for decryption I have
string decryptRotWord(string word, int num)
{
string newWord;
num = num % 26;
int index;
for(int i = 0; i < word[i]; i++)
{
newWord[i] = tolower(word[i]);
if(word[i] >= 'a' && word[i] <= 'z')
{
index = word[i] - num;
if(index < 'a')
index = index + 26;
newWord[i] = index;
}
}
return newWord;
}
however, for this one, it does not output anything when I test with
cout << decryptRotWord("vdds", 2);
In your decrypt function, I think you have a mistake on the loop end condition:
for(int i = 0; i < word[i]; i++)
As in the encrypt function, you should iterate over length
for(int i = 0; i < word.length(); i++)
When your for loop arrives to the letter 'z', it does 'z' + 6. But that goes beyond the max length of a char (127). You get an undefined behavior from that.
You should implement a way of starting to count from 'a' whenever you check the encryption goes beyond 'z'.
For the decryption, same as mentioned before, you want to test :
i < word.length()