In: Computer Science
How do i make this program accept strings?
#include <iostream>
#include <algorithm>
using namespace std;
int binaryToOctal(char digits[], int a)
{
int number;
if (digits[0] == '0')
{
if (digits[1] == '1')
if (digits[2] == '0')
return 2; //found "010"
else
return 3; //found "011"
else if (digits[1] == '0')
if (digits[2] == '1')
return 1; // found "001"
else
return 0; //found "000"
}
else
{
if (digits[0] == '1')
if (digits[1] == '1')
if (digits[2] == '0')
return 6; //found "110"
else
return 7; //found "111"
else if (digits[1] = '0')
if (digits[1] == '0')
if (digits[2] == '1')
return 5; // found "101"
else
return 4; //found "100"
}
}
int main()
{
char arr[] = {'0', '0', '1', '1', '0', '1', '1', '1', '0'};
int n = (sizeof(arr) / sizeof(arr[0]));
cout << "For the input array : ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
cout << "Octal is: ";
int i = n - 1;
string res = "";
//traverse the loop from back and take three values from left to right
while (i >= 0)
{
//for three values
if (i >= 2)
{
char temp[3] = {arr[i - 2], arr[i - 1], arr[i]};
//call function for finding the octal in three
int ans = binaryToOctal(temp, 3);
//appending to new string after converting the integer to char
res += (ans + '0');
i -= 3;
}
//but if only size left with 2 then add one 0 and take two element
else if (i == 1)
{
char temp[3] = {0, arr[i - 1], arr[i]};
//call function for finding the octal in three
int ans = binaryToOctal(temp, 3);
//appending to new string after converting the integer to char
res += (ans + '0');
i -= 2;
}
//similarly add two 0 then one value for size 1 left
else if (i == 0)
{
char temp[3] = {0, 0, arr[i]};
//call function for finding the octal in three
int ans = binaryToOctal(temp, 3);
//appending to new string after converting the integer to char
res += (ans + '0');
i--;
}
else
{
i--;
}
}
//reverse the resultant string to get the octal values
reverse(res.begin(), res.end());
cout << res << endl;
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
int binaryToOctal(char digits[], int a)
{
int number;
if (digits[0] == '0')
{
if (digits[1] == '1')
if (digits[2] == '0')
return 2; //found "010"
else
return 3; //found "011"
else if (digits[1] == '0')
if (digits[2] == '1')
return 1; // found "001"
else
return 0; //found "000"
}
else
{
if (digits[0] == '1')
if (digits[1] == '1')
if (digits[2] == '0')
return 6; //found "110"
else
return 7; //found "111"
else if (digits[1] = '0')
if (digits[1] == '0')
if (digits[2] == '1')
return 5; // found "101"
else
return 4; //found "100"
}
}
int main()
{
// **** I have updated here
string arr;
cout << "Enter the binary string: ";
cin >> arr;
int n = arr.size();
// ****
cout << "For the input array : ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
cout << "Octal is: ";
int i = n - 1;
string res = "";
//traverse the loop from back and take three values from left to right
while (i >= 0)
{
//for three values
if (i >= 2)
{
char temp[3] = {arr[i - 2], arr[i - 1], arr[i]};
//call function for finding the octal in three
int ans = binaryToOctal(temp, 3);
//appending to new string after converting the integer to char
res += (ans + '0');
i -= 3;
}
//but if only size left with 2 then add one 0 and take two element
else if (i == 1)
{
char temp[3] = {0, arr[i - 1], arr[i]};
//call function for finding the octal in three
int ans = binaryToOctal(temp, 3);
//appending to new string after converting the integer to char
res += (ans + '0');
i -= 2;
}
//similarly add two 0 then one value for size 1 left
else if (i == 0)
{
char temp[3] = {0, 0, arr[i]};
//call function for finding the octal in three
int ans = binaryToOctal(temp, 3);
//appending to new string after converting the integer to char
res += (ans + '0');
i--;
}
else
{
i--;
}
}
//reverse the resultant string to get the octal values
reverse(res.begin(), res.end());
cout << res << endl;
return 0;
}
output:-

If you have any doubt, feel free to ask in the comments.