Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

c - Can someone help, i cant fix program that should printf in backwards


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your program is overly complicated and wrong.

You probably want this:

void naopak(char slovo[20])
{
  int i;
  for (i = 0; slovo[i] != ''; i++); // goto end of string

  for (i -= 1; i >= 0; i--)  // i -= 1 decrements i, because i points the NUL terminator
  {
    printf("%c", slovo[i]);
  }
}

int main()
{
  char slova[20];
  scanf("%19s", slova);    // input of word (%19s prevents buffer overflow
                           // if you enter more than 19 chars

  naopak(slova);         // print the word in reverse
  return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...