hello i am new to programming but every time i run this code i get the error "c++ Expression: string subscript out of range" i am pretty sure that the error is in the second for loop
#include<iostream>
#include<string>
#include <algorithm>
using namespace std;
int main()
{
string x;
string n;
cin >> x;
for (int i = 0; i <= x.length(); i++){
if (x[i] == 'a' || x[i] == 'e' || x[i] == 'i' || x[i] == 'o' || x[i] == 'u' || x[i] == 'y')
{
x[i] = ' ';
}
}
x.erase(remove_if(x.begin(), x.end(), isspace), x.end());
int f = x.length() * 2;
for (int i = 0; i <f-1; i+=2){
n[i] = '.';
}
cout << n << endl;
}
for (int i = 0; i <= x.length(); i++)
should be:
for (int i = 0; i < x.length(); i++)
because index starts from 0
x[x.length()] out of range
can not use n[index] when the size of n is 0,use n.push_back()
for (int i = 0; i < x.length(); i++){ //error
if (x[i] == 'a' || x[i] == 'e' || x[i] == 'i' || x[i] == 'o' || x[i] == 'u' || x[i] == 'y')
{
x[i] = ' ';
}
}
x.erase(remove_if(x.begin(), x.end(), isspace), x.end());
int f = x.length() * 2;
for (int i = 0; i <f-1; i+=2){
n[i] = '.'; // n.push_back('.');
}
cout << n <
< endl;
If you are trying to remove all the vowels from your input string, you do not need to run two separate loops. You are already using std::remove_if, just add lambda shown in following code and you will have your desired output.
#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>
using namespace std;
int main() {
std::string str = "This is test of vowels aeiou to be removed. Also the upper case AEIOU.";
std::cout << "Original String: " << str << std::endl;
str.erase(std::remove_if(str.begin(), str.end(), [](char x) {
return (x == 'a'|| x == 'e' || x == 'i' || x == 'o' || x == 'u' || x == 'y'
|| x == 'A'|| x == 'E' || x == 'I' || x == 'O' || x == 'U' || x == 'Y')
}), str.end());
std::cout << "String with vowels removed: " << str << std::endl;
// Not sure what you are trying to achieve with this code.
int f = x.length() * 2;
for (int i = 0; i <f-1; i+=2)
{
n[i] = '.';
}
return 0;
}
Here is the LiveDemo
You have 3 bits of code causing errors:
1) Your first error occurs because you should be checking that i < x.length() in the first loop condition, not that i <= x.length() . Even better, calculate the length once and then use that value in the loop condition to avoid calling x.length() repeatedly. Your first loop should look like this:
int length = x.length();
for (int i = 0; i < x.length(); ++i)
note: ++i is quicker than i++ and in this case using ++i would not make a difference to the logic.
2) Your second error is due to the line int f = x.length() * 2 because you are doubling the length of the array and then using that number to iterate through the array. For example, if your array has a length() of 5 then int f = x.length() * 2 would mean that f = 10 but if the length of the array is 5 than accessing the array with any number greater than 4 (array index starts at zero) would produce an error. In your second loop condition you do this
for (int i = 0; i < f-1; i+=2 ) {
n[i] = '.' // i could easily be more than 4 at this point
}
To fix you second problem, take out the * 2 from int f = x.length() * 2
3) Your third is because you haven't given the n string object a value yet you are accessing it using array indexing []
Related
I was creating the program for my friend who is interested in numerology and wants a way to calculate the value of words faster. But even though there are no errors and/or warnings, but the words' values aren't being calculated correctly. For example, "mark" has a value of 9, but the program shows 2. If you can figure out what the problem is, then pls help me. Thank you so much!
My Code:
#include <iostream>
#include <string>
int value{0};
void clear();
int main(void)
{
int number{0};
std::string response;
bool run = true;
while (run)
{
clear();
value = 0;
number = 1;
response = "";
std::cout << "How many words to evalute?:\n> ";
std::cin >> number;
std::cin.ignore();
clear();
std::string* pPhrase = new std::string[number];
int* pValue = new int[number];
for (int i = 0; i < number; ++i) // could replace "number" with "sizeof(pPhrase)/sizeof(pPhrase[0])"
{
std::cout << "Enter Word #" << i+1 << " (or type your full phrase):\n> ";
std::cin >> pPhrase[i];
for (char j : pPhrase[i])
{
value = 0;
j = std::tolower(j);
if (j == 'a' || j == 'i' || j == 'j'
|| j == 'q' || j == 'y')
value += 1;
if (j == 'b' || j == 'k' || j == 'r')
value += 2;
if (j == 'c' || j == 'g' || j == 'l'
|| j == 's')
value += 3;
if (j == 'd' || j == 'm' || j == 't')
value += 4;
if (j == 'e' || j == 'h' || j == 'n'
|| j == 'x')
value += 5;
if (j == 'u' || j == 'v' || j == 'w')
value += 6;
if (j == 'o' || j == 'z')
value += 7;
if (j == 'f' || j == 'p')
value += 8;
pValue[i] = value;
value = 0;
std::cout << '\n';
clear();
}
}
std::cin.ignore();
std::cin.clear();
std::cout << "\n\n";
for (int i = 0; i < number; ++i)
std::cout << "Value of \"" << pPhrase[i] << "\": " << pValue[i] << '\n';
//std::cin.ignore();
std::cin.clear();
std::cout << "Would you like to evaluate another phrase? (Y/n):\n> ";
std::getline(std::cin, response);
delete[] pPhrase;
delete[] pValue;
if (response[0] == 'y' || response[0] == 'Y'
|| response.empty() || response[0] == ' ')
{
std::cout << "\n\n";
continue;
}
break;
}
std::cout << "Exiting...";
system("killall Terminal");
std::cout << "\n\n\n";
return 0;
}
void clear()
{
system("clear");
}
You could make your program run faster by using a lookup table:
static const int table[26] = {
/* a */ 1, /* b */ 2, /* c */ 3, /* d */ 4, /* e */ 5,
/* f */ 8, /* g */ 3, /* h */ 5, /* i */ 1, /* j */ 1,
/* k */ 2, /* l */ 3, /* m */ 4, /* n */ 5, /* o */ 7,
/* p */ 8, /* q */ 1, /* r */ 2, /* s */ 3, /* t */ 4,
/* u */ 6, /* v */ 6, /* w */ 6, /* x */ 5, /* y */ 1,
/* z */ 7,
};
// ...
if (isalpha(j))
{
const int table_index = j - 'a';
const letter_value = table[table_index];
pValue[i] = letter_value;
//...
}
Print the assembly language for your code fragment, then print the assembly language for the above code fragment. Compare.
Look at all the places where you set value to zero. You do it before main() begins (which is not very useful, actually). Then, bizarrely, you set value to zero at both the start and end of your inner for loop, which means that all the value += statements in that for loop might as well be value = statements. You keep value from accumulating value.
How to solve this? First, get rid of all the value = 0 statements inside your inner for loop. Second, add int value = 0; before the inner for loop, like this:
int value = 0;
for (char j : pPhrase[i])
{
// ...
}
At this point, you might as well get rid of int value{0}; above main(). That step isn't absolutely necessary, but that declaration of value is totally unneeded.
Also, DO NOT put system("killall Terminal"); in your sample code. It's very unfriendly, since it kills the very command line on which you want others to run your sample code.
(In general, using system() at all is a bad idea, since it's unportable.)
There are several issues. The main issue is that on line 70, pValue[i] = value; is incorrectly positioned inside the for loop.
Here is a fixed version:
#include <iostream>
#include <string>
using namespace std;
int value{0};
void clear();
int main() {
int number{0};
string response;
while (true) {
clear();
value = 0;
number = 1;
response = "";
cout << "How many words to evaluate?:\n> ";
cin >> number;
cin.ignore();
clear();
auto *pPhrase = new string[number]{};
int *pValue = new int[number]{};
for (int i = 0; i < number; ++i) // could replace "number" with "sizeof(pPhrase)/sizeof(pPhrase[0])"
{
cout << "Enter Word #" << i + 1 << " (or type your full phrase):\n> ";
cin >> pPhrase[i];
for (char j: pPhrase[i]) {
j = tolower(j);
if (j == 'a' || j == 'i' || j == 'j'
|| j == 'q' || j == 'y')
value += 1;
if (j == 'b' || j == 'k' || j == 'r')
value += 2;
if (j == 'c' || j == 'g' || j == 'l'
|| j == 's')
value += 3;
if (j == 'd' || j == 'm' || j == 't')
value += 4;
if (j == 'e' || j == 'h' || j == 'n'
|| j == 'x')
value += 5;
if (j == 'u' || j == 'v' || j == 'w')
value += 6;
if (j == 'o' || j == 'z')
value += 7;
if (j == 'f' || j == 'p')
value += 8;
cout << '\n';
clear();
}
pValue[i] = value;
value = 0;
}
cin.ignore();
cin.clear();
cout << "\n\n";
for (int i = 0; i < number; ++i)
cout << "Value of \"" << pPhrase[i] << "\": " << pValue[i] << '\n';
cin.clear();
cout << "Would you like to evaluate another phrase? (Y/n):\n> ";
getline(cin, response);
delete[] pPhrase;
delete[] pValue;
if (response[0] == 'y' || response[0] == 'Y'
|| response.empty() || response[0] == ' ') {
cout << "\n\n";
continue;
}
break;
}
cout << "Exiting...";
system("killall Terminal");
cout << "\n\n\n";
return 0;
}
void clear() {
system("clear");
}
I have a question about 935B - Fafa and the Gates in CodeForces. My code is working for the first test cases but it’s getting stuck on test case 20, this is the code I used, could someone please tell me what I’m doing wrong here? Thanks!
#include <iostream>
#include <string>
using namespace std;
int main(){
long long a, x = 0, y = 0, total = 0;
cin >> a;
string s;
cin >> s;
for (long long i = 0; i <= a; i++){
if (s[i] == 'U') x += 1;
if (s[i] == 'R') y += 1;
if (x == y && s[i] == s[i+1]) total += 1;
}
cout << total << endl;
}
Aside from the i<=a issue I called out in the comments above, there's another issue.
Even if you fix the for loop to be stop after i<a, then this statement:
if (x == y && s[i] == s[i+1]) total += 1;
Will still reference an invalid index at s[i+1] since i+1 is an invalid index on the last iteration of the array.
On each iteration of the loop, you need to see if he's at the gate first, then update x or y appropriately, then assess if he changed kingdoms.
If he's at a position x > y, you know he's in the lower kingdom. Likewise if y > x, you know he's in the upper kingdom on the map.
I think this is closer to what you want:
bool topKingdom = false; // initially at 0,0. Fafa is currently in the "lower kingdom" on the map
for (long long i = 0; i < a; i++){
bool atGate = (x == y);
if (s[i] == 'U') y++;
if (s[i] == 'R') x++;
// if we were previously "at a gate", assess if we are now in a new kingdom from before
if (atGate && !topKingdom && y > x) {
topKingdom = true;
total++;
}
else if (atGate && topKingdom && x > y) {
topKingdom = false;
total++;
}
}
cout << total << endl;
I have a project to write a code that performs mathematical operations on matrices. The user inputs a matrix in the form of a string and then inputs an operator, if it's + or - or * or / then the user must enter another matrix...
So I wanted to make a function that performs each operation and I started with ADD function, but I get an error when I call it in the main.
Example of input matrix: [3 4 9;2 5 8;1 2 50]
Note: No extra spaces or semicolons should be printed out.
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <algorithm>
using namespace std;
string ADD (float matrix1,float matrix2,int arraySize);
int main()
{
string s1; //first matrix
char op; //operator
string s2;//second matrix
int y; //for power operation
int n = 0; //number of rows of first matrix
int m = 0; //number of columns of first matrix
int o = 0; //number of rows of second matrix
int p = 0;//number of columns of second matrix
getline(cin, s1);
for (int i = 0; i < s1.size(); i++)
{
if (s1[i] == ';') n++;
}
for (int j = 0; j < s1.size(); j++)
{
if (s1[j] == ' ') m++;
}
n = n+1;
m = (m/n)+1;
s1.erase(0, 1); //to remove first bracket
for (int z = 0; z < s1.size(); z++) //to replace characters with a space
{
if (s1[z] == ';' || s1[z] == ']') s1[z] = ' ';
}
string token1;
float matrix1 [n*m];
for (int x = 0; x < n*m; x++)
{
token1 = s1.substr(0, s1.find(" "));
float v = atof(token1.c_str());
matrix1 [x] = v;
s1.erase(0, s1.find(" ")+1);
}
cout <<"Please Enter An Operator From The Following List: '+ - * ^ T D I /'" <<endl;
cin >> op;
if (op == '+' || op == '-' || op == '*' || op == '/')
{
cin.ignore(); getline (cin,s2);
}
else if (op == '^') cin >> y;
for (int f = 0; f < s2.size(); f++)
{
if (s2[f] == ';') o++;
}
for (int q = 0; q < s2.size(); q++)
{
if (s2[q] == ' ') p++;
}
o = o+1;
p = (p/o)+1;
s2.erase(0, 1); //to remove first bracket
for (int e = 0; e < s2.size(); e++) //to replace characters with a space
{
if (s2[e] == ';' || s2[e] == ']') s2[e] = ' ';
}
string token2;
float matrix2 [o*p];
float h;
for (int c = 0; c <o*p; c++)
{
token2 = s2.substr(0, s2.find(" "));
h = atof(token2.c_str());
matrix2 [c] = h;
s2.erase(0, s2.find(" ")+1);
}
if ( n == o && m == p && op == '+')
{
ADD(matrix1,matrix2,m*n) //Where I got the error Cannot convert 'float*' to 'float' for argument '1'
}
return 0;
}
string ADD (float matrix1[],float matrix2[],int arraySize)
{
string str;
for (int u = 0; u < arraySize; u++)
{
float matrix3[arraySize];
matrix3[u] = matrix1[u] + matrix2[u];
ostringstream ss;
ss << matrix3;
str = ss.str();
return str;
}
cout <<str;
}
Your forward declaration doesn't match your function definition:
string ADD (float matrix1,float matrix2,int arraySize);
and
string ADD (float matrix1[],float matrix2[],int arraySize)
{
...
}
Simply change your forward declaration to match the definition.
Is it possible to reverse an array without affecting the special characters ? By special characters, I mean anything characters not included from 'a' to 'z' and 'A' to 'Z'. I am short of ideas to build the algorithm, I still haven't it figured out.
One simple solution would be to Simple Solution:
1) Create a temporary character array --> ex: myArr[].
2) Copy alphabetic characters from the given array to myArr[].
3) Reverse myArr[] using standard string reversal algorithm.
4) Now traverse input string and myArr in a single loop. Wherever there is alphabetic character is input string, replace it with current character of myArr[].
Little problem with above solution, it requires extra space and it does two traversals of input string.
You can reverse with one traversal and without extra space. Below is algorithm.
1) Let input string be 'str[]' and length of string be 'a'
2) l = 0, r = a-1
3) While l is smaller than r, do following
a) If str[l] is not an alphabetic character, do l++
b) Else If str[r] is not an alphabetic character, do r--
c) Else swap str[l] and str[r]
Here's a solution that will do it "in place" in one pass.
bool isspecial(char c)
{
if ((c >= 'a') && (c <= 'z')) return false;
if ((c >= 'A') && (c <= 'Z')) return false;
return true;
}
void rev(char* array, int N)
{
int i = 0; // i points to the first index of the array
int j = N - 1; // j points to the last index of the array
while (i < j)
{
if (isspecial(array[i]))
{
i++;
}
else if (isspecial(array[j]))
{
j--;
}
else
{
char tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
}
}
Console.WriteLine("enter any string");
string str = Console.ReadLine();
string[] revstr = new string[str.Length];
for (int i = 0; i < str.Length; i++)
{
int ch = Convert.ToInt16(str.ToLower()[i]);
if ((ch < 97 || ch > 122))
{
revstr[i] = str[i].ToString();
}
}
for (int k = str.Length - 1; k >= 0; k--)
{
int ch = Convert.ToInt16(str.ToLower()[k]);
if (!(ch < 97 || ch > 122))
{
for (int j = 0; j < str.Length; j++)
{
if (revstr[j] == null)
{
revstr[j] = str[k].ToString();
break;
}
}
}
}
for (int s = 0; s < revstr.Length; s++)
{
Console.Write(revstr[s]);
}
If you want the position of the special characters to remain the same and the rest of the string to be reversed then this should work -
#include <iostream>
using namespace std;
void swap(char& a, char& b)
{
char temp = a;
a = b;
b = temp;
}
int main()
{
string s = "Hell$o World";
for(int i = 0, j = s.length() -1;i < s.length()/2; i++, j--) {
while((s[i] <= 'a' && s[i] >= 'Z') || s[i] >= 'z' || s[i] <= 'A') {
i++;
}
while((s[j] <= 'a' && s[j] >= 'Z') || s[j] >= 'z' || s[j] <= 'A') {
j--;
}
swap(s[i], s[j]);
}
cout << s << endl; //dlro$W olleH
return 0;
}
For example, how do you count the occurrence of "TJ" in OAEKOTJEOTJ?
if (s[i] == 'TJ') and (s[i] == 'T'+'J')
x += 1;
First one gives me an error, second one doesn't count. I need a beginner solution to this, I haven't learned very much about c++ commands yet. Thanks
int x = 0
string s;
cin >> s;
for (int i = 0; i < 100; i++)
if (s[i] == T || s[i] == t) && (s[i+1] == J || s[i+1] == j)
x += 1
cout << x << endl;
That's the excerpt from my code, it doesn't count any tj, tJ, Tj or TJ
Try using:
if(s[i] == 'T' && s[i+1] == 'J') // and make sure you do not run out of bounds of string with index i.
x += 1;
EDIT:
Based on your code:
int x = 0
string s;
cin >> s;
for (int i = 0; i < 100; i++)
if (s[i] == T || s[i] == t) && (s[i+1] == J || s[i+1] == j)
x += 1
cout << x << endl;
You should do it like following:
int x = 0
string s;
cin >> s;
for (int i = 0; i < s.length()-1; i++) // use size of string s.length()-1 to iterate the string instead of 100
if (s[i] == 'T' || s[i] == 't') && (s[i+1] == 'J' || s[i+1] == 'j') // compare the ascii values of characters like - 'T' 'J' etc.
x += 1
cout << x << endl;
std::string provides a function find which searches the string for substrings, including multi-character substrings (below, I am using C++11 syntax):
#include <iostream>
#include <string>
int main()
{
using namespace std;
string text { "OAEKOTJEOTJ" };
unsigned int occ { 0 };
size_t pos { 0 };
for (;;) {
pos = text.find("TJ",pos); // Search for the substring, start at pos
if (pos == string::npos) // Quit if nothing found
break;
++pos; // Continue from next position
++occ; // Count the occurrence
}
std::cout << "Found " << occ << " occurrences." << std::endl;
}
The way it's done above we advance by one character only after each match. Depending on whether/how we want to deal with overlapping matches, we might want to advance pos by the length of the search pattern. (See chris's comment as well.)
Try this:
#include <locale> // for tolower() function
string tolower(string s) {
tolower(s[0]);
tolower(s[1]);
return s;
}
...
int main() {
string s;
cin >> s;
int n = s.size(),cont = 0;
for(int i = 0; i < n ; ++i) {
if(tolower(s.substr(i,2)) == "tj") {
++cont;
}
}
cout << cont << endl;
return 0;
}