ADT string


 I am creating an abstract data type for strings implementing the string functions but not using the string library. I have my .c file and my .h file below. The problem seems to lie in addMystring I have commented out the area's that seem to have the problem. when they are not commented out I have a segmentation fault. though, it should work, so I believe that theres something wrong with my resizing functions, though I cannot figure out what. If someone could tell me what I am doing wrong I would appreciate it!

# include <stdio.h>
# include <stdlib.h>
# include "string.h"

# define bool int
# define true 1
# define false 0

typedef struct 
{
	int length;
	int capacity;
	char* data;
	}MyString;


int stringlen(char*);
void doubleMyString(MyString*);
void halfMyString(MyString*);
//     Implementation/Process:
//     Takes in an address of a string of type MyString, and sets length to 0, capacity //to 10
//     and data pointing to 10 bytes of memory. If the character array for data isn't //null then
//     true is returned.
bool initMyString( MyString* aStr )
{
	aStr= (MyString*)malloc(sizeof(MyString));
	aStr->length= 0;
	aStr->capacity = 10;
	aStr->data = (char*)malloc(sizeof(char)*10);

	if(aStr->data==NULL)
		return false;
	else
		return true;

}
//If the character string is larger than the capaicity of the MyString then the string is //doubled
void doubleMyString(MyString* aStr)
{
	char* temp;
	int i;

		temp= (char*)malloc(sizeof(char)*aStr->capacity*2);
		
		for( i=0; i< aStr->length; i++)
		{
			temp[i]=aStr->data[i];
		}
		
		temp[i]='\0';

		aStr->capacity*=2;
		
		free( aStr->data); 
		aStr-> data =temp;
		
}

void halfMyString(MyString* aStr)
{
//if the character string is less than 25% of the capacity of the MyString string then //the memory is halved
	int i;
	char* temp;

	temp= (char*)malloc(sizeof(char)*aStr->capacity/2);

	for(i=0; i< aStr->length; i++)

	{
		temp[i]= aStr->data[i];
	}
	
	temp[i] = '\0';

	aStr->capacity/=4;

	free(aStr->data);
	aStr->data= temp;
	
}

bool copyMyString( MyString* aStr, char* cStr)
{
// Takes in a pointer to a MyString string and a pointer to a character string, the //values in the character string are copied into the MyString string.
//True is returned if the string is allocated correctly.
	int i, length;
	
	aStr->data =(char*)malloc(sizeof(char)*10);
	length= stringlen(cStr) +1;

	if (length >= (aStr->capacity-aStr->length))
		while (length <= aStr->capacity- aStr->length)
		{
			doubleMyString(aStr);
		}
	else if (length < (aStr->capacity/4)-aStr->length)
		while(length < (aStr->capacity/4) - aStr->length)
		{		
			halfMyString(aStr);
		}
	
	i= 0; 
	while(cStr[i] !='\0')
	{
		aStr->data[i] = cStr[i];
		i++;
	}
		aStr->data[i]='\0';
		
		if (aStr->data !=NULL)
		return true;
}

bool addMyString(MyString* aStr, char ch)
{
//     Takes in a pointer of a string of type MyString and adds a character to the end of //the string. If there is not enough memory allocated to hold the character then the //capacity is doubled.
	int length= aStr->length;
	printf("%d",aStr->length);
	//aStr->data[length]=ch;
	//aStr->data[length +1]='\0'; 
	aStr->length+=2;
	
	if ((aStr->length) >= (aStr->capacity))
	{
		//doubleMyString(aStr);
	}

	if (aStr->data!=NULL)
		return true;
}

bool catMyString( MyString* aStr , char* cStr)
{
//     Takes in a pointer of a string of type MyString and a pointer to a string and 
//     concatenates the string to the end of the MyString string. If MyString isn't null
//     true is returned. 
	int i;
	for(i=0 ; i< stringlen(cStr); i++)
	{	
		addMyString(aStr, cStr[i]);
	}
	
	aStr->data[i] = '\0'; 

	if ( aStr->data != NULL)
		return true;
		
}

bool readMyString( MyString* aStr, FILE* inFile, char delimit, int number)
{

//     Takes in a pointer of a string of type MyString and a pointer to a file, the //characters from a file are read into the MyString, if there is not enough space, the //string is doubled. If the string isn't null, true is returned. 
	int i=0;

	while((getc(inFile)!=EOF)|(i==number))
	{
		addMyString(aStr,getc(inFile));
		i++;
	}

	if ( aStr->data != NULL)
		return true;
}

void destroyMyString( MyString* aStr)
{
//     Takes in a pointer of a string of type MyString and frees the memory allocated for
//     the string and sets the data members to their initial states. 
	free(aStr->data);
}

int stringlen(char* Str)
{
	int i=0;

	while(Str[i] != '\0')
	{
		i++;
	}
	
	return i;
}
(sumber) https://www.daniweb.com/
Artikel Selanjutnya Artikel Sebelumnya
Post Terkait :
Struktur Data