
-----------------------------------
mathware
Thu May 19, 2016 5:05 pm

Insertion and deletion operations on strings
-----------------------------------
I need help with the following program:
[code]#include
#include
#include
 
typedef struct
{
    char *str;
}STRING;
 
//initializes dynamic string
void init(STRING *s)
{
    char str[10];
    s->str=(char *)calloc(strlen(str)+1,sizeof(char));
    strcpy(s->str,str);
}
 
//appends a character at the end of STRING
void append_c(STRING *s,char c)
{
    int capacity=10,used=0;
    s->str=(char *)malloc((capacity+1)*sizeof(char));
    int i;
    for(i=0;istr=(char *)realloc(s->str,sizeof(char) * (capacity+1));
       }
       s->str[used]=c;
       s->str[used+1]=0;
       used++;
    }
}
 
//concatenates string 'str' to STRING
void append_s(STRING *s,char *str)
{
    init(s);
    strcat(s->str,str);
}
 
//inserts the string 'str' to STRING at position 'pos'
//'pos' is valid if 0 len)
        pos=len;
 
    strncpy(temp,s->str,pos+1);
    temp[pos+1]=0;
    strcat(temp,str);
    strcat(temp,s->str+pos+1);
 
}
 
//removes 'n' characters from STRING starting from 'pos'
//0str; temp++)
        len++;
    temp=(char *)malloc((len+1) * sizeof(char));
 
    if(pos < 0 || pos > len)
        return;
    int x=len-pos;
 
    if(n str,pos);
    temp[pos]=0;
    strcat(temp,s->str+pos+n);}
 
    else{
    strncpy(temp,s->str,pos);
    temp[pos]=0;}
}
 
 
int main()
{
    STRING s;
    init(&s);
 
    append_s(&s,"GRAMMING");
    printf("%s\n",s.str);
 
    insertion(&s,0,"PRO");
    printf("%s\n",s.str);
 
    insertion(&s,12,"L");
    printf("%s\n",s.str);
 
    append_c(&s,'A');
    printf("%s\n",s.str);
 
    append_s(&s,"NGUAGES");
    printf("%s\n",s.str);
 
    deletion(&s,1,4);
    printf("%s\n",s.str);
 
    free(s.str);
 
    return 0;
}[/code]

Output should be:
[code]//GRAMMING
//PROGRAMMING
//PROGRAMMING L
//PROGRAMMING LA
//PROGRAMMING LANGUAGES
//PAMMING LANGUAGES[/code]

Functions for insertion and deletion don't work.
Could someone correct possible syntax and logical errors?
