Detect first letter problem

Hi forum! I was writeing a encryption program, i make it all to convert car something like: 01010 10101 01111

The program is encrypting something like:
10101 01010 01111 -acr

I need a funtion or something to take care of first letter after next after next unit the end. So there's something like that? Thanks in advance!

*sorry for my english*
*im begginner*

1
2
3
4
5
6
7
8
9
10
11
cin >> a; //car
cout<< "Encrypting now!"<<endl;

//The message encrypting engine
//The engine is something like: detecting a.....found...print 10101
//How i need to be: detecting first letter.......c.....print 01010

cout<< "Result: "<<endl;
cout<< "10101 - a"<<endl;
cout<< "01010 - c"<<endl; //Theres the problem
cout<< "01111 - r"<<endl;
Last edited on
You haven't shown the type of a. Is it a std::string or a char array?

In any case, you need to know the length of a;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  unsigned len;
  len = strlen(a);  // if char array
  len = a.size();  // if string

  //  Loop through the word
  for (unsigned i=0; i<len i++)
  {  if (a[i] == 'a')
        cout << "10101 ";
     if (a[i] == ''c')
       cout << "01010 ";
    if (a[i] == 'r')
      cout << "01111 ";
  }
  cout << endl; 


Of course you're going to need an if statement for every possible letter you want to encode. A table would work better.
Last edited on
Topic archived. No new replies allowed.