我想生成代码; 输入句子时,系统将仅显示这些句子的辅音字母。
我生成了代码,但该代码无法识别特殊的土耳其语元音字母 就像İ,Ü,Ö作为常数字母一样。仅识别英语拉丁元音字母。
我该如何解决?
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
int vowel (char);
int main ()
{
setlocale(LC_ALL, "Turkish");
char s[100], t[100];
int c, d = 0;
printf ("Enter sentence vowels will ve removed:\n");
gets (s);
for (c = 0; s[c] != '\0'; c++)
{
if (vowel (s[c]) == 0)
{
t[d] = s[c];
d++;
}
}
t[d] = '\0';
strcpy (s, t);
printf ("Letters without vowels: %s\n", s);
return 0;
}
int vowel (char ch)
{
if (ch == 'a' || ch == 'A'
|| ch == 'e' || ch == 'E'
|| ch == 'ı' || ch == 'I'
|| ch == 'i' || ch == 'İ'
|| ch == 'o' || ch == 'O'
|| ch == 'ö;' || ch == 'Ö'
|| ch == 'u' || ch == 'U'
|| ch == 'ü' || ch == 'Ü')
return 1;
else
return 0;
}
答案 0 :(得分:1)
我将其重写为使用wchar_t
而不是char。
Ascii字符以单个字节(字符)表示。您可能需要将代码转换为使用wchar_t
。
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
int vowel (wchar_t); // <---- accepts wchar_t instead of char
int main ()
{
setlocale(LC_ALL, "Turkish");
/* To get it to work on https://www.onlinegdb.com/
* use setlocale(LC_ALL,"en_US.UTF-8");
*
*/
wchar_t s[100], t[100]; // <---- wchar_t instead of char
int c, d = 0;
printf ("Enter sentence vowels will ve removed:\n");
// TODO : also check the return code. On error, it returns NULL.
fgetws (s, 100, stdin); // <----- accepting wchar_t string
for (c = 0; s[c] != '\0'; c++)
{
if (vowel (s[c]) == 0)
{
t[d] = s[c];
d++;
}
}
t[d] = '\0';
// using memcpy instead of strcpy.
// also see - http://www.cplusplus.com/reference/cwchar/wcscpy/
memcpy (s, t, sizeof(t));
printf ("Letters without vowels: ");
for (c = 0; c < d; c++)
putwchar(s[c]); // <---- print wchar_t character on terminal
putwchar('\n');
return 0;
}
int vowel (wchar_t ch)
{
// I've prefixed an L - more than one byte.
if (ch == L'a' || ch == L'A'
|| ch == L'e' || ch == L'E'
|| ch == L'ı' || ch == L'I'
|| ch == L'i' || ch == L'İ'
|| ch == L'o' || ch == L'O'
|| ch == L'ö' || ch == L'Ö'
|| ch == L'u' || ch == L'U'
|| ch == L'ü' || ch == L'Ü')
return 1;
else
return 0;
}
样本输出
$ gcc test.c
$ ./a.out
Enter sentence vowels will ve removed:
abcdxyÖÜ
Letters without vowels: bcdxy
答案 1 :(得分:0)
亲爱的@algrebe非常感谢您的快速回复。我在https://onlinegdb.com/BkxnoKZ2L上运行过代码,但是土耳其语元音在onlinegdb上显示为问号
Enter sentence vowels will ve removed:
avbcdxyÖÜ
setters without vowels: vbcdxys��