好的,方法 myName()
是完全错误的。我是 C 编码新手。不知何故,我需要遍历字符串 *theName
这样我就可以评估其中的每个字符。我需要在保持char *name[1] = {David $ MN % Baez}
的同时做到这一点作为歌曲索引数组。帮助我被困住了!
#include <stdio.h>
int main()
{
char *name[1] = {"David $ MN % Baez"} ; // should work
myName(name) ;
return 0 ;
}
void myName(char *theName)
{
for(int i = 0; i < sizeof(theName); i++)
{
if("aeiouAEIOU".IndexOf(theName) >= 0)/////// is vowel
{
printf("character [ %c ] located at position %d is a vowel", theName[i], i);
}
if(theName == ' ')//////
{
printf("character [ %c ] located at position %d is a space", theName[i], i);
}
else if(theName == '$' || theName == '%')/////////////////////
{
printf("character [ %c ] located at position %d is a symbol", theName[i], i);
}
else
{
printf("character [ %c ] located at position %d is a consonant", theName[i], i);
}
}
}
最佳答案
几个问题:
char *name[1]
是char *
的数组,有点像char name[10][1]
但可以动态分配。根据你的描述我 认为你只需要一维数组,所以只使用char *
或char[]
就可以了。sizeof
运算符用于查询对象或类型的大小,如果您 如果想要获取字符串的长度,请使用strlen
。- 我不知道
IndexOf
来自哪里,也许其他语言有 这个方法,但是在C中没有。在你的代码中if("aeiouAEIOU".IndexOf(theName)
,我想您想查找是否 字符是元音。尝试使用string.h
中的一些 API。 for (int i=0; ...
这是C++语法,在C中,定义变量out用于
。- 在
printf
中使用\n
来格式化输出。
代码供您引用:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void myName(char *theName);
int main()
{
char name[] = {"David $ MN % Baez"} ; // should work
myName(name) ;
return 0 ;
}
void myName(char *theName)
{
char vowel[] = "aeiouAEIOU";
int i=0;
for(i = 0; i < strlen(theName); i++)
{
if(strchr(vowel, theName[i]))/////// is vowel
{
printf("character [ %c ] located at position %d is a vowel\n", theName[i], i);
}
if(theName == ' ')//////
{
printf("character [ %c ] located at position %d is a space\n", theName[i], i);
}
else if(theName == '$' || theName == '%')/////////////////////
{
printf("character [ %c ] located at position %d is a symbol\n", theName[i], i);
}
else
{
printf("character [ %c ] located at position %d is a consonant\n", theName[i], i);
}
}
}
关于c - C中遍历字符串中的字符进行分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22830398/