Adding a char at the end of every line in while loop

时间:2017-06-04 23:51:31

标签: c

Can someone please help me with this problem, I want my program to print + with every end of line.

My code is:

#include <stdio.h>
#include <string.h>

int main()
{

char str[65];
char * pch;
char str1[65];

fgets (str, 100, stdin); 

  pch = strtok (str," ");
  while (pch != NULL)
  {
   str1=pch;
    printf("=");
    printf ("%s\n",pch);
    pch = strtok (NULL, " ");
  }

return 0;
}

With input, "Muhannad Stack" (output wrong):

=Muhannad
=Stack

Correct:

=Muhannad+
=Stack+

Therefore, my problem is how to add + at the end of every printed line

1 个答案:

答案 0 :(得分:0)

以下是可行的:

#include <stdio.h>
#include <string.h>    
int main()
{    
  char str[65];
  char * pch;
  char str1[65];      
  fgets (str, 100, stdin);       
  pch = strtok (str,"\n");
  pch = strtok (pch," ");
  while (pch != NULL) {
    printf ("=%s+\n",pch);
    pch = strtok (NULL, " ");
  }
  return 0;
}