c - how to get input char from user? -
so i'm supposed use while loop read 2 strings , operations strings. user should able enter multiple string pairs until enters exit. how accept string instead of hard copying function.?
int main(int argc, const char *argv[]) { char string[3], stringsec[2]; string[0] = 'c'; string[1] = 'a'; string[2] = 't'; stringsec[0] = 'c'; stringsec[1] = 's'; int array[3][4]; // functions... return 0; }
use fgets read string , strcmp check if equals "exit":
while (strcmp(string, "exit")) { fgets(string, sizeof(string), stdin); fgets(secstring, sizeof(secstring), stdin); // perform operations on strings } explanation:
strcmpreturns 0 if 2 strings match. in example above loop continue long doesn't return 0.fgetsreads amount of bytes (2nd argument) file (3rd argument) string (1st argument).
notes:
fgetsnot remove trailing newline string read. can remove addingstring[strlen(string)-1] = '\0';after reading.- you must
#include <string.h>usestrcmp.
Comments
Post a Comment