Take number by number of a sequence of numbers to compare them

1

My teacher of algorithms and programming techniques spent an exercise in which a = "123567348" and of that I would say which numbers are even and which are prime. He said he would have to use substr() , and passed an example:

#include <stdio.h>
#include <string>
int main ()
{
int c;
char a [] = "123456789", pedaco;
for (c = 1; c <= 10 ; c++) 
pedaco = a.substr(c,1);
  return 0;
}

but returns the error:

  

In the example he gave it: pedaco = substr(a,c,1); and also did not work.

    
asked by anonymous 15.10.2018 / 22:57

1 answer

3

You are mixing C with C ++ and I think this is the problem, says that you were told to use substr() , it only makes sense to use the native of C ++ and not use the C to create something like a string . If it were pure C there is substr() . It would be like this:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string a = "123456789";
    for (int c = 0; c < 10; c++) cout << a.substr(c, 1);
}

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

If you did not need this method you could do better:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string a = "123456789";
    for (int c = 0; c < 10; c++) cout << a[c];
}

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

But in C ++ it would look even better:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string a = "123456789";
    for (char& c : a) cout << c;
}

See running on ideone . And no Coding Ground . Also I put in GitHub for future reference .

    
15.10.2018 / 23:06