从 C 中的字符串中删除元音
2021-07-07 09:05:31
深夜i
--
--
从
的
字
符
串
中
删
除
元
音
从字符串中删除或删除元音的 C 程序。 如果输入是“C programming”,则输出是“C prgrmmng”。
在程序中,我们创建一个新字符串并逐个字符处理输入字符串。 如果存在元音,我们将其排除,否则我们复制它。 输入结束后,我们将新字符串复制到其中。 最后,我们得到一个没有元音的字符序列。
从字符串中删除元音的C程序
#include <stdio.h>
#include <string.h>
int check_vowel(char);
int main()
{
char s[100], t[100];
int c, d = 0;
printf("Enter a string to delete vowels\n");
gets(s);
for (c = 0; s[c] != '\0'; c++) {
if (check_vowel(s[c]) == 0) { // If not a vowel
t[d] = s[c];
d++;
}
}
t[d] = '\0';
strcpy(s, t); // We are changing initial string. This is optional.
printf("String after deleting vowels: %s\n", s);
return 0;
}
int check_vowel(char t)
{
if (t == 'a' || t == 'A' || t == 'e' || t == 'E' || t == 'i' || t == 'I' || t =='o' || t=='O' || t == 'u' || t == 'U')
return 1;
return 0;
}
程序输出:
使用指针的 C 编程代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int check_vowel(char);
main()
{
char string[100], *temp, *p, ch, *start;
printf("Enter a string\n");
gets(string);
temp = string;
p = (char*)malloc(100);
if( p == NULL )
{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
start = p;
while(*temp)
{
ch = *temp;
if ( !check_vowel(ch) )
{
*p = ch;
p++;
}
temp++;
}
*p = '\0';
p = start;
strcpy(string, pointer); /* If you wish to convert original string */
free(p);
printf("String after removing vowel(s): \"%s\"\n", string);
return 0;
}
int check_vowel(char a)
{
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return 1;
else if (a == 'A' || a == 'E' || a == 'I' || a == 'O' || a == 'U')
return 1;
else
return 0;
}
上一篇:
idea打包java可执行jar包
下一篇:
C 子串,C 中的子串
评论区