Hi,i'm given a number and a string.I have to check if i can separate the string in pieces equal to the number and if i can i print yes or no.To separate a string the start of the new it must be different from the starting letter of the previous string.This is my code and i have a problem on a test that it's given number 2 and string aaacas and the correct output is YES aaa cas. My code prints Yes aa acas.
Thank you.
First of all, your code is much longer than it should be. It uses 48 lines of code when it should really use around 30 (not counting empty lines). Not really sure why you were storing every single character in the alphabet in an array. Here's a very simplified version that works:
#include <iostream>
#include <string>
usingnamespace std;
int main (int argc, char** argv) //These values aren't used in this program,
//the reason they are here is because my editor automatically inserts the
//template which has these command-line parameters. Ignore these.
{
string A;
size_t N;
unsignedint counter = 0; //Always have this initialized, especially if it is a counter variable.
cin >> N;
cin >> A;
cin.clear(); //These two calls are to
cin.ignore();//clear the error flags since it already has an object.
if(A.size() % N == 0)
{
cout << "Yes:" << endl;
for (constauto& i : A)
//This is a range-based for loop, available since the C++11 standard.
//If your compiler doesn't support this, use a nested for loop instead.
{
cout << i;
counter++;
if (counter == (A.size() / N))
{
counter = 0;
cout << endl;
}
}
} else {
cout << "No";
}
return 0;
}
You're welcome :)
EDIT:
Also, your code is very hard to read. You should space your code lines out a little bit and add spaces between conditionals, preprocessor directives, and statements to improve the legibility. From your code:
1 2 3 4 5 6 7 8 9
#include<iostream>
#include<string>
usingnamespace std;
int main(){
bool Alphabet[26];
string A;
int N,i,counter;
char a;
...
vs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
bool Alphabet[26];
string A;
int N, i, counter;
char a;
...