![]() |
|
|
#1 (permalink) |
|
Junkie
|
[C] Help with conversion
The problem I'm trying to solve, involves writing a function named convertToBinaryVersionA that will allow a user to enter a string, and convert the string to a binary string.
So for instance, my ouput would look like the following: Enter a string: 010011001 010011001 is convertible! or Enter a string: ^^^^010011001 010011001 is convertible! ^= denotes a user hitting the spacebar or Enter a string: 010011001^^^^ 010011001 is convertible! ^= denotes a user hitting the spacebar Enter a string: abc01010 abc01010 is not convertible! So far, this what I have, I haven't actually worked out how to ignore spaces before or after a string. Can anyone give me a hint on what I should do? 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 input string: ");
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;
}
|
|
|
|
|
#2 (permalink) |
|
Devils Cabana Boy
Location: Central Coast CA
|
trim! i don't remember the syntax but the function trim will remove leading and trailing spaces for you.
__________________
Donate Blood! "Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen |
|
|
| Tags |
| conversion |
|
|