samedi 25 avril 2015

A C code to reverse a string

Does anybody see the bug here? I have got alternate solutions.

int main()
{

  char line[100], res[100], temp[20];
  fgets( line, 100*sizeof(char), stdin);    
  int i, j, l;
  for( i=strlen(line)-1; i>=0; i--)
  {
    if(line[i]==' ')
        for(j=i+1, l=0; line[j]!=' ' && line[j]!='\0'; l++, j++)
            temp[l]=line[j];
    temp[++l]=' ';
    temp[++l]='\0';
    strcat(res, temp);
  }
  puts(res);
  return 0;
}

Aucun commentaire:

Enregistrer un commentaire