strcmp function doesn't work while comparing string and user input which includes Turkish characters

谁说胖子不能爱 提交于 2019-12-25 09:01:07

问题


char *x="Çankırı";
char *y=malloc(sizeof(char)*25);
scanf("%s",y);

if(strcmp(x,y) == 0)
    printf("A");
else
    printf("%s",y);

I enter 'Çankırı' for y, but else part runs. How can I compare these strings?

Windows10 , codeblocks.

EDIT: I found a clue. Problem is about setlocale function. When I use setlocale(LC_ALL,"TURKISH"), one of the string doesn't work fine(Output is not Çankırı, Ank2r2), and If I use setlocale(LC_ALL,"C"), other one doens't work fine. I don't know how to fix it.


回答1:


You probably need to use functions that accept 'wide' characters. For example:

#include <wchar.h>
wchar_t *x=L"Çankırı";
wchar_t y[25];
wscanf(L"%s",y);

if(wcscmp(x,y) == 0)
    wprintf(L"A");
else
    wprintf(L"%s",y);


来源:https://stackoverflow.com/questions/42542177/strcmp-function-doesnt-work-while-comparing-string-and-user-input-which-include

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!