getchar is nothing to do with the problem which you had.
ssharish
Printable View
getchar is nothing to do with the problem which you had.
ssharish
Now i post the hole code from my program. I use realloc function to get as memory as exactly the programm needs.The programm works but i will post it because maybe somebody else want to see the finally answer.
The exercise says this:
make a function with arguments two strings and returns a new string which has the characters from first string that not included in second string.
example: If str1="hello" and str2="halla" then the string that returns the function should be str3="eo".
the function:
Now i will give some code that you can test if the function worksCode:char *fun(char *s1, char *s2)
{
int i,j,l;/* i,j for for loops and l for the array*/
int found;/*flag*/
int slen1,slen2;
char *k=NULL;/* we set the pointer we return with NULL*/
slen1=strlen(s1);
slen2=strlen(s2);
found=0;
l=0;
for(i=0; i<slen1; i++)
{
found = 0;
for(j=0; j<slen2; j++)
{ if(s1[i]==s2[j]) /*for each element of s1[i] i check if there is also in s2*/
{ found=1; break;}
}
if (found==0)
{
k=(char *)realloc(k,sizeof(int));
k[l++]=s1[i];
}
}
return k;
}
Another more difficult answer is on this postCode:#include <stdio.h>
#include <string.h>
char *fun(char *s1, char *s2);
int main(void)
{
char *p1="hello";
char *p2="hallo";
char *l;
l=fun(p1,p2);
printf(l);
free(l);/*note that we free the memory*/
return 0;
}
http://cboard.cprogramming.com/showp...8&postcount=10
from Bokarinho