Thank You Rekna. I liked the code that you had where you test for lowercase q or uppercase Q, that was something that I didn't think off.
I also ran into a problem, when I added the while loop, I wasn't able to ask the user the size of the pointer after the first iteration.
So my output looked like this:
=====First Time Run==========
Enter an estimate of size of string: 7
Enter a binary string to continue or q to quit:
123ab46
The given string is not convertible!
======Second Time Run==========
Enter a binary string to continue or q to quit: 7
123ab46
The given string is not convertible!
Here's my latest code, hopefully I'm doing something right here:
Code:
#include<stdio.h>
#include<stdlib.h>
void convertToBinaryVersionA();
int main(void)
{
convertToBinaryVersionA();
return 0;
}
void convertToBinaryVersionA()
{
char *cPtr;
int iSize;
int i;
char *cBinary;
int j = 0;
int iCount;
printf("Enter an estimate of size of string: ");
scanf("%d", &iSize);
getchar();
cPtr = (char *) malloc (iSize * sizeof(char));
printf("Enter a binary string to continue or q to quit: ");
gets(cPtr);
cBinary = (char *) malloc (iSize * sizeof(char));
for (i = 0; i < iSize; i++)
{
if( * (cPtr + 0) == 'q' || * (cPtr + 0) == 'Q')
{
printf("Thank You!\n");
}
else if ( * (cPtr + i) == '0' || * (cPtr + i) == '1' )
{
cBinary[j] = cPtr[i];
j++;
iCount = 0;
}
else
{
iCount = 1;
}
}
if ( iCount == 0 )
{
printf("The converted string is: %s", cBinary);
}
else
{
printf("The inputed string is not convertible!\n");
}
return;
}