Esempio n. 1
0
//用三反转等效左移字符串(st与ed之间,包含st不包含ed的内容)  
//n是进行循环移位的位数
char* str_shl(char* st, char* ed, int n)
{
	str_rev(st, &st[n]);
	str_rev( &st[n], ed);
	str_rev(st, ed);
	return st;
}
Esempio n. 2
0
void add(char *p1,char *p2,char *p3)
{
      int i;
      int j;
      int LenA;
      int LenB;
      int c;
      int rem=0;
      LenA=strlen(p1);
	LenB=strlen(p2);
      c = LenA>=LenB ? LenA : LenB;
	str_rev(p1);
	str_rev(p2);
	for(i=0;i<c;i++)
	{
	      if(i>=LenA) p1[i]='0';
	      else if(i>=LenB) p2[i]='0';
	      p3[i]=(p1[i]-'0'+p2[i]-'0'+rem)%10+'0';
	      rem=(p1[i]-'0'+p2[i]-'0'+rem)/10;
	}
	if(rem) p3[i++]=rem+'0';
	p3[i]='\0';
	while(p3[--i]=='0') p3[i]='\0';
	p1[LenA]='\0';
	p2[LenB]='\0';
	str_rev(p1);
	str_rev(p2);
	str_rev(p3);
	return;
}
Esempio n. 3
0
void add(char *ptr1,char *ptr2,char *ptr3)
{
    int len_of_a,len_of_b,i,j,counter,rem=0;
    len_of_a=strlen(ptr1);
    len_of_b=strlen(ptr2);
    if(len_of_a>=len_of_b)
    {
        counter=len_of_a;
    }
    else
    {
        counter=len_of_b;
    }

    str_rev(ptr1);
    str_rev(ptr2);
    for(i=0,j=0; (i<=counter-1); i++,j++)
    {

        if(i>=len_of_a)
        {
            ptr1[i]='0';
            ptr1[i]-=48;
        }
        else
        {
            ptr1[i]-=48;
        }
        if(j>=len_of_b)
        {
            ptr2[j]='0';
            ptr2[j]-=48;
        }
        else
        {
            ptr2[j]-=48;
        }
        ptr3[i]=(ptr1[i]+ptr2[j]+rem)%10;
        ptr3[i]+=48;
        rem=(ptr1[i]+ptr2[j]+rem)/10;
    }
    if(rem!=0)
    {
        ptr3[counter]=rem;
        ptr3[counter]+=48;
        ptr3[counter+1]='\0';
    }
    else
    {
        ptr3[counter]='\0';
    }
    str_rev(ptr3);
    return;
}
void multi(char *s1,char *s2)
{
    int i,j,l1,l2,num1,num2;
    char ans[S]= {0};

    str_rev(s1);
    str_rev(s2);

    l1=strlen(s1);
    l2=strlen(s2);

    for(i=0; i<l2; i++)
    {
        num2=s2[i]-'0';

        for(j=0; j<l1; j++)
        {
            num1=s1[j]-'0';

            ans[i+j]+=num1*num2;

            if(ans[i+j]>9)
            {
                ans[i+j+1]+=ans[i+j]/10;
                ans[i+j]%=10;
            }
        }
    }

    for(i=0; i<(l1+l2); i++)
    {
        sum_fac[count]+=ans[i];
        ans[i]+='0';
    }
    ans[i]=0;

    str_rev(ans);
    str_rev(s2);

    while(ans[0]=='0')
    {
        for(i=0; i<(l1+l2); i++)
        {
            ans[i]=ans[i+1];    /* setting number  in actual order */
        }
    }

    strcpy(a[count],ans);
}
Esempio n. 5
0
int main() {

    char char_arr[30];
    gets(char_arr);

    puts(str_rev(char_arr));
}
Esempio n. 6
0
File: 263.c Progetto: ksyu/uvajudge
int main()
{
    int n, m, cur, clen;
    char dig[16];

    scanf("%d", &n);
    while(n > 0)
    {
        printf("Original number was %d\n", n);
        
        clen = 0;
        cur = n;
        while(is_appeared(cur, clen) == FALSE)
        {
            chain[clen] = cur;

            my_itoa(cur, dig);

            qsort(dig, strlen(dig), sizeof(dig[0]), dcmp);
            n = atoi(dig);
            
            str_rev(dig);
            m = atoi(dig);

            cur = n - m;

            printf("%d - %d = %d\n", n, m, cur);
            clen++;
        }
        printf("Chain length %d\n\n", clen);
        scanf("%d", &n);
    }
    return 0;
}
Esempio n. 7
0
void multiply(char *p1,char *p2,char *p3)
{
      int i;
      int j;
      int k;
      int l;
      int rem;
      int t;
      int z;
      int LenA=strlen(p1);
      int LenB=strlen(p2);
      strcpy(b,"0");
      for(i=LenB-1,l=0;i>=0;i--,l++)
      {
      	rem=0;
      	for(j=LenA-1,k=0;j>=0;j--,k++)
      	{
      		a[k]=((p1[j]-'0')*(p2[i]-'0')+rem)%10+'0';
      		rem=((p1[j]-'0')*(p2[i]-'0')+rem)/10;
      	}
      	if(rem) a[k++]=rem+'0';
      	a[k]='\0';
      	str_rev(a);
      	for(z=0;z<l;z++) a[k++]='0';
      	a[k]='\0';
      	add(a,b,c);
      	strcpy(b,c);
      }
      strcpy(p3,b);
      return;
}
Esempio n. 8
0
File: 1504.c Progetto: Jing0/ACM
int main(){
	int i,t;
	char a[100],b[100],sum[100];
	scanf("%d",&t);
	while(t--){
		scanf("%s%s",a,b);
		strcpy(sum,"0");
		str_rev(a);str_rev(b);
		add(sum,a);
		add(sum,b);
		str_rev(sum);
		for(i=0;sum[i]=='0';++i);
		char *p=sum+i;
		printf("%s\n",p);
	}
	return 0;
}
Esempio n. 9
0
int main()
{
    char s[MAX_STR_LEN], str[MAX_STR_LEN];
    gets(s);
    puts(str_rev(str, s));
    puts(str);
    return 0;
}
Esempio n. 10
0
File: 3982.c Progetto: Jing0/ACM
void add(char *sum,char *str){
	int i,sign=0,len_sum=strlen(sum),len_str=strlen(str);
	int len_max=(len_sum>len_str)?len_sum:len_str;
	int len_min=(len_max==len_sum)?len_str:len_sum;
	char *p=(len_sum>len_str)?str:sum;
	str_rev(sum);str_rev(str);
	for(i=len_min;i<len_max;++i)
		*(p+i)='0';
	for(i=0;i<len_max;++i){
		sum[i]+=str[i]-'0'+sign;
		sign=(sum[i]>'9');
		sum[i]-=10*sign;
	}
	if(sign)
		sum[i++]='1';
	sum[i]='\0';
	str_rev(sum);
}
Esempio n. 11
0
void str_rev(char *s)
{
		if (*s == 0)
				return;
		else {
				str_rev(s+1);
				printf("%c",*s);
		}
}
//Convierte un numero unsigned long long int a una string
void ullToStr(unsigned long long int n, char **str, int str_size) {
  int i = 0;
  while((i<(str_size-1)) && (n>0)) {
    (*str)[i]=n%10+48;
    n=trunc(n/10);
    i++;
  }
  (*str)[i]=0;
  *str=str_rev(*str);
}
char* sum(char l[MAX],char f[MAX])
{
	int l1,l2,a,b,c,i,sum;
	char answer[MAX]={0},s1[MAX]={0},s2[MAX]={0};

	strcpy(s1,l);
	strcpy(s2,f);

	str_rev(s1);
	str_rev(s2);

	l1=strlen(s1);
	l2=strlen(s2);

	if(l1 > l2){
		a=l1-l2;
		for(b=1; b<=a; b++,l2++)s2[l2]='0';
	}
	if(l1 < l2){
		a=l2-l1;
		for(b=1; b<=a; b++,l1++)s1[l1]='0';
	}

	c=0;
	for(i=0; s1[i]; i++){
		sum=s1[i]+s2[i]-96+c;
		if(sum>9){
			sum=sum-10;
			c=1;
		}
		else c=0;
		answer[i]=sum+48;
	}
	if(c==0)answer[i]=0;
	else{
		answer[i]=c+48;
		answer[i+1]=0;
	}

	str_rev(answer);

	return answer;
}
Esempio n. 14
0
/*
*@Name:XAlignSuffixY
*@Parameter:seq1 with its index, seq2 and xsufixy to store the results
*@Return: 1 ok, 0 error.
*/
int XAlignSuffixY(char *seq1, int index, char *seq2, float *xsuffixy)
{
    int n_index = strlen(seq1) - index+1;
    //int n = strlen(seq2);
    //float xsuffixy[n+1];

    char tmp1[strlen(seq1)];
    tmp1[strlen(seq1)]='\0';

    char tmp2[strlen(seq2)];
    tmp2[strlen(seq2)]='\0';

    memcpy(tmp1, seq1,sizeof(tmp1));//we don't want seq1 and seq2 reversed really
    memcpy(tmp2, seq2,sizeof(tmp2));

    //use XAlignPrefixY to execute backward dynamic programming
    XAlignPrefixY(str_rev(tmp1), n_index, str_rev(tmp2), xsuffixy);

    return 1;
}
Esempio n. 15
0
void Binary(int n)
{
      int i=0;
      while(n)
      {
      	a[i]=48+(n%2);
      	i++;
      	n/=2;
      }
      a[i]='\0';
      str_rev();
      return;
}
Esempio n. 16
0
int main()
{

    char str[] = "Please reverse this string!";

    printf("%s\n", str);

    str_rev(str);

    printf("%s\n", str);

    return 0;
}
Esempio n. 17
0
void main()
{
	long n;

	while(scanf("%ld",&n)==1)
	{
		if(n==0)break;

		make_binary(n);

		str_rev();

		printf("The parity of %s is %d (mod 2).\n",s,p);
	}
}
Esempio n. 18
0
/****************************************************************************
*
*       Function Name   : main
*       Description     : Demonstration basic string concepts
*       Returns         : Success or Failure
*
****************************************************************************/
int main()
{
 char input[NAMESZ]; /* Variable to get input from user */
 char reverse[NAMESZ]; /* Variable to store reverse of string */
 char *fgets_ret = NULL; /* Return Value from fgets */
 /* Initializing the local variables */
 memset(input, 0, NAMESZ);
 memset(reverse, 0, NAMESZ);

 printf("Enter a string (Size < 32)\n");
 /* Observe the waring given by compiler for the following line.
    Compiler gives the warning since there is no array bound check for gets */
 /* Same issue is there with scanf also */
 // gets(input);
 
 /* fgets has an option to specify the array bound */ 
 fgets_ret = fgets(input, NAMESZ,stdin); 
 
 if(NULL != fgets_ret)
 { 
  /* fgets reads a maximum of NAMESZ-1 characters. Last position is reserved for
    '\0'. But there could be a '\n' before '\0' if the number of characters
    were less than NAMESZ-1. So we need to remove it */ 
  remove_newline(input);

  /* reversing the string */
  str_rev(input, reverse);
  printf("Original string is %s\n", input);
  printf("Original string is %s\n", reverse);
 
  /* Checking for palindrome */
  palindrome_check(input,reverse);
 }
 else
 {
  printf("fgets failed\n");
 }
 return 0;
}
Esempio n. 19
0
int main(int c, char **argv)
{
	int x;
	printf("enter integer...\n");
	scanf("%d", &x);
	char *str = malloc(SIZE * sizeof(char));
	if (str == NULL) {
		printf("unable to allocate memory");
		exit(0);
	}
	int count = 0;
	if (x < 0) {
		str[count] = '-';
		count++;
		x *= -1;
	}

	count = itoarev(str, x, count);

	printf("before str rev %s\n", str);
	str_rev(str, count);
	printf("itoa is %s and sizeof int is %lu", str, sizeof(int));
	return 0;
}
Esempio n. 20
0
/**
* USAGE: ./a.out string1 
**/
int main(int argc, char *argv[]){
	if(argc<2) return 1;
	str_rev(argv[1]);
	printf("%s",argv[1]);
	return 0;
}
Esempio n. 21
0
/*
*@Name:traceBack
*@Parameter:Tpath, int *, char *, char *
*@Parameter:Tpath store the points in optimal alignments, two seqs is the original sequences
*@Return: 1 ok, 0 false
*/
int traceBack(Tpath *path, int *pindex, char *seq1, char *seq2)
{
    int number = *pindex;//assign the value of path size
    //printf("number=%d,size=%d,sort first\n",number, sizeof(path[0]));

    qsort(path, number, sizeof(path[0]), comp);//sort the points first

    //add the last node, as last node didn't store in path before when doing alignment
    (path+number)->seq2i = strlen(seq2);
    (path+number)->seq1j = strlen(seq1);
    (*pindex)++;
    number++;
    printf("Total path size=%d\n",*pindex);

    //int l=0;
    //for (l=0;l<number;l++)
    //{
    //    printf("(%d,%d)\n",(path+l)->seq2i,(path+l)->seq1j);
    //}

    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //char align_seq1[1000];//care of the memory
    //char align_seq2[1000];//!!!!!!!!!!!!!!!!!!!
    //char alignMark[1000];//!!!!!!!!!!!!!!!!!!!
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    char *align_seq1=(char*)malloc(SEQ_LENGTH*sizeof(char));
    char *align_seq2=(char*)malloc(SEQ_LENGTH*sizeof(char));
    char *alignMark=(char*)malloc(SEQ_LENGTH*sizeof(char));

    float score=0;//record the final score of optimal alignment
    int k=0, i=0;
    for(k=number-1;k>0;k--)
    {
        //printf("k=%d\n",k);
        int cur_x=(path+k)->seq2i;
        int prev_x=(path+k-1)->seq2i;

        int cur_y=(path+k)->seq1j;
        int prev_y=(path+k-1)->seq1j;

        //printf("(%d,%d) (%d,%d)\n",cur_x,cur_y,prev_x,prev_y);
        //((path+k)->seq2i = (path+k-1)->seq2i) and ((path+k)->seq1j =1 + (path+k-1)->seq1j) then a gap in seq2
        if( (cur_x == prev_x)&&(cur_y == prev_y+1) )//a gap in seq2
        {
            score--;
            align_seq1[i] = seq1[cur_y-1];
            align_seq2[i] = '-';
            alignMark[i]=' ';
            //printf("-%c-\n",align_seq1[i]);
            //printf("-%c-\n",align_seq2[i]);
            i++;
        }

        //(path+k)->seq1j = (path+k-1)->seq1j and ((path+k)->seq2i =1 + (path+k-1)->seq2i)then a gap in seq2
        else if( (cur_y == prev_y)&&(cur_x == 1 + prev_x) )// a gap in seq1
        {
            score--;
            align_seq2[i] = seq2[cur_x-1];
            align_seq1[i] = '-';
            //printf("-%c-\n",align_seq1[i]);
            //printf("-%c-\n",align_seq2[i]);
            alignMark[i]=' ';
            i++;
        }
        else{//match or mismatch
            //printf("no gap\n");
            align_seq1[i] = seq1[cur_y-1];
            align_seq2[i] = seq2[cur_x-1];
            //printf("-%c-\n",align_seq1[i]);
            //printf("-%c-\n",align_seq2[i]);
            //printf("no gap\n");
            if (seq1[cur_y-1] == seq2[cur_x-1])//match
            {
                score++;
                alignMark[i]='*';
            }
            else//mismatch
            {
                score--;
                alignMark[i]=' ';
            }
            i++;
        }
    }//end trace back cycle

    //-------------------------------------------------
    //*************now write alignment to file*********
    //-------------------------------------------------

    align_seq1[i]='\0';
    align_seq2[i]='\0';
    alignMark[i]='\0';
    //printf("now i=%d\n",i);
    //printf("%s\n",align_seq1);
    //printf("%s\n",(align_seq2));
    //printf("%s\n",(alignMark));

    //reverse sequences first.
    //printf("before str_rev:%s\n",align_seq1);
    str_rev(align_seq1);
    //printf("after str_rev:%s\n",align_seq1);

    //printf("before str_rev:%s\n",align_seq2);
    str_rev(align_seq2);
    //printf("after str_rev:%s\n",align_seq2);

    str_rev(alignMark);

    printf("-----write alignment to file...\n");
    FILE *fp = fopen("align_result.txt","w");
    if(fp==NULL)
    {
        printf("open align_result.txt file error\n");
        return 0;
    }
    int u=0;
    i=strlen(align_seq1);// now i is the number of chars we need to write to file
    //printf("strlen i=%d",i);
    //printf("out ok,%d, %s\n",strlen(sub1),sub1);
    while(u*LINE_LEN<i)
    {
        //printf("ok u=%d\n",u);
        //char s2[LENGTH];
        //char s3[LENGTH];
        int m=0;
        if((i-LINE_LEN*u)<LINE_LEN)
        {
            for(m=LINE_LEN*u;m<i;m++)
            {
                fwrite(&align_seq1[m],1,1,fp);
            }
            fwrite("\n",1,1,fp);

            for(m=LINE_LEN*u;m<i;m++)
            {
                fwrite(&align_seq2[m],1,1,fp);
            }
            fwrite("\n",1,1,fp);

            for(m=LINE_LEN*u;m<i;m++)
            {
                fwrite(&alignMark[m],1,1,fp);
            }
            fwrite("\n",1,1,fp);
        }else
        {
            for(m=LINE_LEN*u;m<LINE_LEN*(u+1);m++)
            {
                fwrite(&align_seq1[m],1,1,fp);
            }
            fwrite("\n",1,1,fp);

            for(m=LINE_LEN*u;m<LINE_LEN*(u+1);m++)
            {
                fwrite(&align_seq2[m],1,1,fp);
            }
            fwrite("\n",1,1,fp);

            for(m=LINE_LEN*u;m<LINE_LEN*(u+1);m++)
            {
                fwrite(&alignMark[m],1,1,fp);
            }
            fwrite("\n",1,1,fp);
        }
        fwrite("\n",1,1,fp);
        u++;
    }
    fclose(fp);

    //printf("final score is:%0.2f\n",score);

    free(align_seq1);
    free(align_seq2);
    free(alignMark);
    return 1;
}
void big_multiplication(char *s1,char *s2)
{
	char ans[2*MAX]={0};
	char M[MAX][MAX]={0};
	int i,j,c,l1,l2,l3,L,line,num1,num2;

	str_rev(s1);
	str_rev(s2);

	l1=strlen(s1);
	l2=strlen(s2);

	for(i=0; i<l2; i++)
	{
		num2=s2[i]-'0';
		c=0;

		for(j=0; j<l1; j++)
		{
			num1=s1[j]-'0';

			M[i][j]=num1*num2+c;
			ans[i+j]+=num1*num2;

			if(ans[i+j]>9)
			{
				c = M[i][j]/10;
				M[i][j] = M[i][j]%10;

				ans[i+j+1]+=ans[i+j]/10;
				ans[i+j]%=10;
			}
			else{
				c=0;
			}
		}
		if(c){
			M[i][j]=c;
		}
	}

	/*making string with trailing 0*/
	for(i=0; i<l2; i++)
	{
		for(j=0; j<l1+1; j++)
		{
			M[i][j]+='0';
		}
	}

	for(i=0; i<(l1+l2); i++)
	{
		ans[i]+='0';
	}

	/*reversing string*/
	for(i=0; i<l2; i++)
	{
		str_rev(M[i]);
	}

	str_rev(ans);
	str_rev(s1);
	str_rev(s2);

	/*delet the front 0*/
	for(i=0; i<l2; i++)
	{
		while(M[i][0]=='0')
		{
			for(j=0; j<(l1+1); j++)
			{
				M[i][j]=M[i][j+1];
			}
		}
	}

	while(ans[0]=='0')
	{
		for(i=0; i<(l1+l2); i++)
		{
			ans[i]=ans[i+1];
		}
	}

	if(!strcmp(ans,""))
	{
		strcpy(ans,"0");
	}

	line=0;
	for(i=0; i<l2; i++)
	{
		if( strcmp(M[i],"") )
		{
			line+=1;
		}
	}

	l3=strlen(ans);
	L=l1 > l2 ? l1 : l2;

	/*printing string*/

	if(!strcmp(ans,"0"))
	{
		for(i=L; i>l1; i--)
			printf(" ");
		printf("%s\n",s1);

		for(i=L; i>l2; i--)
			printf(" ");
		printf("%s\n",s2);

		for(i=0; i<L; i++)
			printf("-");
		printf("\n");

		for(i=L; i>1; i--)
			printf(" ");
		printf("0\n");
	}
	else if(line==1)
	{
		for(i=l3-l1; i>0; i--)
			printf(" ");
		printf("%s\n",s1);

		for(i=l3-l2; i>0; i--)
			printf(" ");
		printf("%s\n",s2);

		for(i=l3-L; i>0; i--)
			printf(" ");

		for(i=0; i<L; i++)
			printf("-");
		printf("\n");

		printf("%s\n",ans);
	}
	else
	{
		for(i=l3-l1; i>0; i--)
			printf(" ");
		printf("%s\n",s1);

		for(i=l3-l2; i>0; i--)
			printf(" ");
		printf("%s\n",s2);

		for(i=l3-L; i>0; i--)
			printf(" ");

		for(i=0; i<L; i++)
			printf("-");
		printf("\n");

		for(i=0; i<l2; i++)
		{
			if(strcmp(M[i],""))
			{
				for(j=0; j < (l3 - strlen(M[i]) - i ); j++)
					printf(" ");

				printf("%s\n",M[i]);
			}
		}

		for(i=0; i<l3; i++)
			printf("-");
		printf("\n");

		printf("%s\n",ans);
	}

}
Esempio n. 23
0
void print_even(char* pLeftPart)
{
	printf("%s",pLeftPart);
	str_rev(pLeftPart);
	printf("%s\n",pLeftPart);
}
Esempio n. 24
0
void print_odd(char* pLeftPart)
{
	printf("%s",pLeftPart);
	str_rev(pLeftPart);
	printf("%s\n",pLeftPart+1);
}
Esempio n. 25
0
/******************************************************************************
*
*       Function Name  : tok_rev_concat
*       Description    : Tokenizes the input string and concatenate the reverse
*			 of each token
*       Returns        : SUCCESS or FAILURE
*
*******************************************************************************/
tok_ret tok_rev_concat(
			IN char input[], /* Input Array */	
			OUT char **arg_result /* Program output */
		      )
{
 char *word = NULL, /* Tokenized words */
      *rev_word = NULL, /* Reverse of each word */
      *temp = NULL; /* temporary variable for realloc */
 int len = 0, /* string length of each word */
     total_len = 1; /* Sum of lengths of all tokenized words. Initialized to 1
                       for '\0' */
 tok_ret ret_val = SUCCESS;
 word = strtok(input, " ,."); /* Tokenizing the first word */
 if(NULL != word) /* Checking whether any word is there */
 {
    len = strlen(word); /* Length of the first word */
    total_len = total_len + len; /* Adding to total length */
  
     /*Allocating memory for result variable */ 
    *arg_result = (char *)malloc(total_len * sizeof(char));
    if(NULL == *arg_result) /* Error handling for malloc */
    {
        printf("Malloc failed\n");
        return FAILURE;
    }
    /* Finding out the reverse of the first word */
    ret_val = str_rev(word, &rev_word);
    if(FAILURE == ret_val) /* Error handling for str_rev */
    {
        printf("Reversing failed\n");
        return FAILURE;
    }
 
    /*Copying the first string to result. strcpy is used because we allocated 
    enough memory. */ 
    strcpy(*arg_result, rev_word);
    /* Freeing the memory allocated to reverse after copying its value to result*/
    free(rev_word);
    rev_word = NULL;
 }
 /* Loop to perform the taks on all words */
 while(NULL != word)
 {
    word = strtok(NULL, " ,.");  /* Tokenizing subsequent words */
    if(NULL == word)
    {
       break;
    }
    len = strlen(word); /* String length of the word */
    if(len > 0) /* Checking for a empty string */
    {
       total_len = total_len + len; /* Adding string length to total length */ 
 
        /* Extending memory to contain the new string also */
       temp = (char *)realloc(*arg_result, total_len * sizeof(char));
       if(NULL == temp) /* Error handling for realloc */
       {
          printf("realloc failed\n");
          return FAILURE;
       }
       *arg_result = temp; /* Get rid of realloc issue */
   
        /* Reversing the word */
       ret_val = str_rev(word, &rev_word);
       if(FAILURE == ret_val) /* Error handling for str_rev */
       {
           printf("Reversing failed\n");
           return FAILURE;
       }
       /* Concatenating the reverse of the new token to result. strcat is used
      because enough memory is allocated */
      strcat(*arg_result, rev_word);
   
   /* Freeing the memory of reverse after concatenating it to result */
      free(rev_word);
      rev_word = NULL;
  }
 }
 return SUCCESS;
}
Esempio n. 26
0
int main(int argc, char *argv[]) {
    int sockfd;
    /*   
    struct addrinfo {
        int              ai_flags;
        int              ai_family;
        int              ai_socktype;
        int              ai_protocol;
        socklen_t        ai_addrlen;
        struct sockaddr *ai_addr;
        char            *ai_canonname;
        struct addrinfo *ai_next;
    };
    */
    struct sockaddr_in servaddr;
    int servaddr_len = sizeof(servaddr);

    int port = SERV_PORT;
    if(argc == 2) {
        port = atoi(argv[1]);
    }

    // socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd == -1) {
        err_die("socket");
    }
    bzero(&servaddr, 0);
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(port);
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    // bind
    if(bind(sockfd, (struct sockaddr *)&servaddr, servaddr_len) < 0) {
        err_die("bind");
    }
    // listen
    if(listen(sockfd, BACKLOG_NUM) < 0) {
        err_die("listen");
    }
    printf("server running on port [%d]\n", SERV_PORT);
    int acceptfd;
    // 阻塞等待accept
    while(1) {
        // accept, 第三个参数是指针!(坑!)
        acceptfd = accept(sockfd, (struct sockaddr*)&servaddr, &servaddr_len);
        if(-1 == acceptfd) {
            perror("accept");
        } else {
            printf("accept from client, acceptfd is %d\n!", acceptfd);
        }

        // read
        char buff[1024];
        memset(buff, '\0', sizeof(buff));
        ssize_t n;
        // 不用要strlen(buff), 编译时就要确定,用sizeof! 
        while( (n = read(acceptfd, buff, sizeof(buff)) )  > 0) {
            printf("read from client:\"%s\"\n", buff);
            // reverse string
            str_rev(buff, 0, strlen(buff)-1);
            printf("write to client: \"%s\"\n", buff);
            // write
            write(acceptfd, buff, strlen(buff));
            // 每次read前把buff清洗下
            memset(buff, '\0', sizeof(buff));
        }
        printf("read from client, body len is 0, closing acceptfd!\n");
        
        // close acceptfd
        close(acceptfd);
    }
    // close sockfd
    close(sockfd);
    
    return 0;
}
Esempio n. 27
0
main()
{
		char s[] = "hello";
		str_rev(s);
	//	printf("
}