Hi all forum members, this is my first post, usually I'd always found an answer somewhere across the web but this time I just can't understand what is the problem. I hope one of you can help !
I'm trying to sort a string separated by commas alphabetically. so I've used recursion in order to iterate. each time the function will assign two pointers. and then, using strcmp It'll compare those strings. a replacment will be made strcmp () will return a value > 0.
I have to mention that it is a draft and I really don't mind regarding the resources. and important of all It does work ! but not always. for the string attached in the text file I'm getting a stack overflow error probably becuase I've used malloc().
I'm using redirection...
Thank you in advance, waiting for the light...
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *sort(char *ptr, char *start, char *buf)
{
char *ptr2;
char *ptr3;
int compare = 0;
/*printf("MAIN string is: %s and length is %d\n",start, strlen(start));*/
printf("ttt\n");
printf("string is: %s and length is %d\n",ptr, strlen(ptr));
/* check if string contains at least one comma*/
if ((strchr(ptr, ',')) != NULL)
{
printf("length of ptr: %d\n",strlen(ptr));
/* allocate memory stream */
ptr2 = malloc(strlen(ptr));
ptr3 = malloc(strlen(ptr));
printf("contains 2nd comma? %s\n",strstr(strstr(ptr, ",")+1,","));
/* check if stream contains a second coma */
if (strstr(strstr(ptr, ",")+1,",") != NULL)
{
printf("length to copy: %d\n",strlen((ptr)) - strlen((strstr(ptr,","))));
/* ptr 2 in the first word to compare */
ptr2 = strncpy(ptr2,ptr,strlen(ptr)-strlen(strstr(ptr, ",")));
*(ptr2+strlen(ptr)-strlen(strstr(ptr, ","))) = '\0';
/* ptr 2 in the first word to compare */
ptr3 = strncpy(ptr3,(strstr(ptr, ","))+1,strlen(strstr(ptr, ",")) - (strlen(strstr(strstr(ptr, ",")+1,",")))-1);
*(ptr3+ strlen(strstr(ptr, ",")) - (strlen(strstr(strstr(ptr, ",")+1,",")))-1) = '\0';
}
else
{
printf("no second comma - else\n");
ptr3 = strncpy(ptr3, strstr(ptr, ",")+1, strlen(strstr(ptr, ",")));
*(ptr3+strlen(strstr(ptr, ","))) = '\0';
ptr2 = strncpy(ptr2, (ptr),strlen(ptr) - strlen(strstr(ptr, ",")));
*(ptr2+strlen(ptr) - strlen(strstr(ptr, ","))) = '\0';
}
compare = strcmp(ptr2,ptr3);
printf("ptr2 is %s ptr3 is %s and compare is %d\n",ptr2,ptr3, compare);
if (compare>0)
{
printf("--SHOULD BE REPLACED--.\n");
printf("MAIN string is: %s and length is %d\n",start, strlen(start));
printf("length to start: %d length of replace: %d\n",strlen(start),strlen(strstr(start, ptr)));
if (strstr(strstr(ptr, ",")+1,",") != NULL)
{
printf("MAIN string is: %s and length is %d\n",start, strlen(start));
if (strlen(start) - strlen(strstr(start, ptr))>0)
{
printf("length of compared strings is not equal\n");
printf("MAIN string is: %s and length is %d\n",start, strlen(start));
printf("buffer is %s\n",buf);
buf = strncpy(buf, start, strlen(start) - strlen(strstr(start, ptr)));
printf("MAIN string is: %s and length is %d\n",start, strlen(start));
printf("%s\n",buf);
*(buf+strlen(start) - strlen(strstr(start, ptr))) = '\0';
printf("MAIN string is: %s and length is %d\n",start, strlen(start));
buf = strcat(buf, ptr3);
printf("%s\n",buf);
buf = strcat(buf, ",");
buf = strcat(buf, ptr2);
printf("%s\n",buf);
if (strstr(strstr(ptr, ",")+1,",") != NULL)
{
printf("2 commas exists in string\n");
buf = strcat(buf, strstr(strstr(ptr, ",")+1,","));
}
else
{
if (strstr(ptr, ",")!=NULL)
{
printf("1 comma exists in string\n");
printf("%s\n",ptr);
printf("ptr %s\n",strstr(ptr, ","));
printf("start: %s\n",strstr(start, ptr));
buf = strcat(buf, strstr(start, ptr)+1);
}
else
{
printf("no commas exists in string\n");
buf = strcat(buf, strstr(ptr, ","));
}
}
printf("%s\n",buf);
*(buf+strlen(start)) = '\0';
}
else
{
printf("length of compared strings is equal\n");
buf = strcpy(buf, ptr3);
printf("buffer is %s\n",buf);
buf = strcat(buf, ",");
buf = strcat(buf, ptr2);
printf("buffer is %s\n",buf);
buf = strncat(buf, strstr(strstr(ptr, ",")+1,","),strlen(strstr(strstr(ptr, ","),",")));
*(buf+strlen(ptr3)+strlen(ptr2)+strlen(strstr(strstr(ptr, ","),","))) = '\0';
printf("buffer is %s\n",buf);
}
}
else
{
printf("There\n");
buf = strncpy(buf, start, strlen(start) - strlen(strstr(start, ptr)));
*(buf+strlen(start) - strlen(strstr(start, ptr))) = '\0';
buf = strcat(buf, ptr3);
buf = strcat(buf, ",");
buf = strcat(buf, ptr2);
}
strcpy(start,buf);
printf("buffer is %s\n",buf);
sort(buf,buf,start);
}
printf("no problema\n");
sort(strstr(ptr, ",")+1,start,buf);
}
return buf;
}
int main()
{
int max = 0;
char *ptr;
char *buf;
/* allocate stream in memory */
ptr = malloc(sizeof(char)*(max));
/* get max size from user*/
scanf("%d\n", &max);
/* store C string in allocated stream */
if (fgets(ptr, max, stdin) != NULL)
{
*(ptr+strlen(ptr)-1) = '\0';
buf = malloc(strlen(ptr)+1);
strncpy(buf, ptr,strlen(ptr));
*(buf + strlen(ptr)) = '\0';
printf("%s",sort(ptr, ptr, buf));
}
return 0;
}