一个字符串中的元音和辅音的Objective-C计数数量
问题描述:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSString * iphone= @"The designs of the iPhone 6 and iPhone 6 Plus were influenced by that of the iPad Air, with a glass front that is curved around the edges of the display, and an aluminum rear that contains two plastic strips for the antenna; both models come in gold, silver, and space gray finishes";
return 0;
}
请帮助我找到不使用数组的Objective-C。一个字符串中的元音和辅音的Objective-C计数数量
答
没人会为你编码。所以,你可以尝试以下步骤:
- 遍历字符串
-
写元音的条件,并通过1
增加计数如果(元音) { 计数++ }
-
最后从字符串长度减去元音计数得到辅音
NSString * string = @“This是测试字符串“; int count = 0; for(int index = 0; index < string.length; index ++){ char ch = [string characterAtIndex:index]; if(ch =='a'|| ch =='e'|| ch =='i'|| ch =='o'|| ch =='u'){ count ++; (@“元音:%d”,count); NSLog(@“consonants:%d”,string.length - count);
答
/* you may check this code may be it will be helpful for you*/
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
char s[100],ch;/*here declare s[100] as array of character and ch defines the character*/
int i,vowel_count,con_count;/* vowel_count and con_count(consonant count) declared here as a integer*/
printf("Enter the string\n");
gets(s);
vowel_count = con_count = 0;
for(i=0;i<strlen(s);i++)
{
if(isalpha(s[i]))/*here we check that string is alphabet or not*/
{
ch = tolower(s[i]);/*conversion of character to small letter*/
if (ch=='a'||ch=='e'|| ch=='i'||ch=='o'||ch=='u')/* check whether the character is vowel or not,(a,e,i,o,u) is vowel*/
vowel_count++;/*if a,e,i,o,u will found in string then vowel count is increased*/
else
con_count++;/* if consonant is found then count of consonant will incresde*/
}
}
printf("No.of vowels = %d\n",vowel_count);
printf("No.of consonants=%d\n",con_count);
}
告诉我们,你已经取得了一些努力,这种自己动手解决。 – 2015-02-11 05:24:49
我看到这个确切的字符串在其他问题中弹出...这是一些某种大型机构的作业吗?哈哈 – Fonix 2015-02-11 06:02:49
请帮我做到这一点 – appintosh 2015-02-11 06:05:48