Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 06-04-2007, 10:42 PM   #1 (permalink)
Junkie
 
zero2's Avatar
 
[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;
}
zero2 is offline  
Old 06-05-2007, 05:56 AM   #2 (permalink)
Devils Cabana Boy
 
Dilbert1234567's Avatar
 
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
Dilbert1234567 is offline  
Old 06-05-2007, 09:51 PM   #3 (permalink)
Junkie
 
zero2's Avatar
 
I've never heard of trim before, but it sounds like its exactly what I need.
zero2 is offline  
Old 06-07-2007, 10:49 AM   #4 (permalink)
Crazy
 
Daemon1313's Avatar
 
Location: Atlanta
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.
__________________
A clear conscience is usually the sign of a bad memory.
Daemon1313 is offline  
 

Tags
conversion


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 07:46 AM.

Tilted Forum Project

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