Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [C] Help with conversion (https://thetfp.com/tfp/tilted-technology/118965-c-help-conversion.html)

zero2 06-04-2007 10:42 PM

[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;
}


Dilbert1234567 06-05-2007 05:56 AM

trim! i don't remember the syntax but the function trim will remove leading and trailing spaces for you.

zero2 06-05-2007 09:51 PM

I've never heard of trim before, but it sounds like its exactly what I need.

Daemon1313 06-07-2007 10:49 AM

pure C does not have a trim function. You just need another else if conditional to handle the space. Currently you're program thinks its an invalid character.


All times are GMT -8. The time now is 11:19 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76