我正在编写一个计算空间和元音的程序而且它不起作用,我想我做了一个无限循环。我会告诉你我的代码:
int contar_p(char a[100]) {
int i = 0, spaces = 1;
while (a[i] != '\0' && i < 100) {
if (a[i] == ' ') {
spaces += 1;
i++;
}
}
return spaces;
}
int contar_v(char b[100]) {
int i = 0, counter = 0;
while (b[i] != '\0' && i < 100) {
if (b[i] == 'a' || b[i] == 'e' || b[i] == 'i' || b[i] == 'o' || b[i] == 'u') {
counter += 1;
}
i++;
}
return counter;
}
int main(void){
char phrase[100];
int words = 0, vowels = 0;
printf("write a phrase ");
gets(phrase);
palabras = contar_p(phrase);
vocales = contar_v(phrase);
printf("%d\n", words);
printf("%d", vowels);
return 0;
}
答案 0 :(得分:1)
循环
i++
是一个无限循环。将if
放在while (a[i]!='\0'){ // No need of condition i < 100
if(a[i]==' '){
spaces+=1;
}
i++;
}
之外。将其更改为
Person *aPerson = [[Person alloc] init];
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:@"First line"];
[myArray addObject:@"Second line"];
[myArray addObject:@"Third line"];
[myArray addObject:@"Fourth line"];
[myArray addObject:@"Fifth line"];
答案 1 :(得分:0)
也许另一种方法可以帮助你更容易地理解事物,我的意思是你知道有A,E,I,O,U也不仅仅是a,e,i,o,u。你永远不应该使用get来代替使用fgets,无论如何看看下面的程序:
#include <stdlib.h>
#include <stdio.h>
void countVowels(char* array){
int i,j,v;
i=0;
int count = 0;
char vowel[]={'a','e','i','o','u','A','E','I','O','U'};
while(array[i]!='\0'){
for(v=0;v<10;v++){
if (array[i]==vowel[v]){
j=i;
while(array[j]!='\0'){
array[j]=array[j+1];
j++;
}
count++;
i--;
break;
}
}
i++;
}
printf("Found %d Vowels\n",count);
}
void contar_p(char a[100]) {
int i = 0, spaces = 0;
for(i=0;a[i]!='\0';i++){
if(a[i]==' ')
spaces++;
}
printf("Found %d Spaces\n",spaces);
}
int main(void){
char a[]="aa bb EOU cc ii";
countVowels(a);
contar_p(a);
return 0;
}
输出:
Found 7 Vowels Found 4 Spaces