我目前正在用C语言编写字典程序。如何在stdin上检测空字符串?使用search_for作为输入。
void find_track(char search_for[])
{
int i;
for (i = 0; i < 5; i++) {
if (strstr(tracks[i], search_for)){
printf("The meaning of %s: %s\n",tracks[i], meaning[i]);
break;
}
}
if (!strstr(tracks[i], search_for)) {
printf("%s could not found in dictionary.\n",search_for);
}
}
再次,我如何使用tolower函数降低转换的输入?
int main()
{
int setloop =1;
titlemessage();
do {
char search_for[80];
char varchar;
printf("Search for: ");
fgets(search_for, 80, stdin);
if(search_for[strlen(search_for)-1]=='\n')
search_for[strlen(search_for)-1]='\0';
find_track(search_for);
printf("Press ENTER to start new search\n");
//printf("Press 'q' to exit the program\n\n");
varchar = getchar();
if (varchar == 10) {
continue;
}else {
break;
}
} while (setloop = 1);
return 0;
}
任何方法将不胜感激。
在C中的stdin和tolower函数上检测到空字符串
fgets(search_for, 80, stdin);
if(search_for[strlen(search_for)-1]=='\n')
search_for[strlen(search_for)-1]='\0';
if(strlen(search_for)==0)
{
// empty string, do necessary actions here
}
char ch;
tolower()
如果ch是大写字母且具有小写字母等效项,则将ch转换为其小写形式。如果无法进行这种转换,则返回的值将保持不变。
for(i = 0; search_for[i]; i++){
search_for[i] = tolower(search_for[i]); // convert your search_for to lowercase
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句