Home > Back-end > Multiple input string, the output of the shortest string
Multiple input string, the output of the shortest string
Time:10-27
Include #include Intmain () { Charstr [80], min [80]; Intk, len; Printf (" input a string: \ n "); Gets (STR); Strcpy (min, STR); Len=strlen (min). While (STR [0]!='\ 0') //to an empty string as input an end tag { K=strlen (STR); If (k{ Len=k; Strcpy (min, STR); } Gets (STR); } Printf (" len=% d, min=% s \ n ", len, min); Return0; } could help explain
CodePudding user response:
#include #include Int main () { The char STR [80], min [80]. Int k, len; Printf (" input a string: \ n "); Gets_s (STR);//input the first string Strcpy (min, STR);//will be the first string as the shortest string, in order to compare the length of the input string behind the relative size of the Len=strlen (min).//record the shortest length of the string While (STR [0]!='\ 0')//when enter empty row by STR string is \ "0", \ 0 is an invisible characters, as a string of ending flag { Gets_s (STR);//input can actually put here K=strlen (STR);//get the current string of length k If (k & lt; Len)//k to compare the length of the shortest string, if less than, STR is the shortest string { Len=k;//update the shortest length Strcpy (min, STR);//update the shortest string } } Printf (" len=% d, min=% s \ n ", len, min); return 0; }
CodePudding user response:
You what the input string, the blank space exactly calculating do not calculate string content Look at your text, the string is separated with a space, but the code you use '\ 0' when space is not right, space is' ', ASCII is 0 x20; '\ 0' is the symbol of the end of the string, ASCII is 0
Gets a reading a line, the space also read in the If the space is separator, there is no gets directly the scanf (" % s ", STR); Good, met the scanf Spaces, TAB and enter the automatic return a string
Feeling is mixed up Spaces and the end of the string tags' \ 0 'difference, and gets the scanf difference