编写一个递归函数来计算数组中的元音
Writing a recursive function that counts vowels in an array
这是我到目前为止的进展,我一直收到这个错误"Assignment makes pointer from integer without a cast" 如果有人更正我的代码并告诉我我的缺陷在哪里,我将不胜感激。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int vowels(char *str[],int len)
{
int i,s=0;
if (!str || len<1)
return s;
for(i=0;i<len;i++){
if (str[i]='a') || (str[i]='e') || (str[i]='i') || (str[i]='o') || (str[i]='u')
|| (str[i]='A') || (str[i]='E') || (str[i]='I') || (str[i]='O') || (str[i]='U'){
return s++;
}
}
return s;
}
int main (void)
{
int i,len;
int pom;
char *str[10];
len=strlen(*str);
for(i=0;i<len;i++){
scanf("%s",str[i]);
pom=vowels(str,len);
printf("%d",pom);
}
return 0;
}
if (str[i]='a')
应该是
if (str[i]=='a')
等等...
还有:
return s++;
应该是
s++;
以防递归很重要(注意签名更改):
int vowels(char *str,int len)
{
if (!str || len<1)
return 0;
if ((*str=='a') || (*str=='e') || (*str=='i') || (*str=='o') || (*str=='u') || (*str=='A') || (*str=='E') || (*str=='I') || (*str=='O') || (*str=='U'))
return 1 + vowels( str+1, len-1 );
else
return vowels( str+1, len-1 );
}
更新:因为这个答案被接受但它没有完全回答问题(它不包含函数vowels()
)我添加了我的递归版本函数 vowels()
:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* Return the number of vowels in a string.
*
* The characters treated as vowels are:
* 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'.
* The function checks the first character of the input string
* then calls itself recursively for the rest of the string.
*
* @param string str the string to check for vowels
* @return int the number of wovels found in the string
*/
int vowels(char *str)
{
/* The number of vowels found in the string so far */
int count = 0;
/* Recursion stop condition: when the string is empty */
if (! str[0]) {
/* An empty string does not contain any vowels */
return count;
}
/* Check the first character of the string */
if (strchr("aeiouAEIOU", str[0])) {
/* Found a vowel, count it */
count ++;
}
/* Recursive call to find the number of vowels after the first character */
return count + vowels(&str[1]);
}
int main(void)
{
int i, len;
int pom;
char *str[10];
int count = sizeof(str)/sizeof(str[0]);
for (i = 0; i < count; i ++) {
/* Hopefully you will not input strings longer than 254 characters */
str[i] = (char *)malloc(255);
fgets(str[i], 255, stdin);
pom = vowels(str[i]);
printf("%d\n", pom);
/* Release the memory */
free(str[i]);
}
return 0;
}
#include <stdio.h>
#include <ctype.h>
int vowels(char *str, int count){
if (!*str)
return count;
char ch = tolower(*str);
int match = ch=='e' || ch=='a' || ch=='i' || ch=='o' || ch=='u';
return vowels(++str, count + match);
}
int main (void){
int i, t, pom;
char str[32];
scanf("%d", &t);
for(i=0;i<t;i++){
scanf("%31s",str);
pom=vowels(str, 0);
printf("%d\n",pom);
}
return 0;
}
如何编写递归函数
Type function_name ( Parameter declarations ) {
if( Termination condition of recursion ) {
return [ End value ];
}
else {
Calculation for the next call;
//Tail recursion call optimization can be expected by the simple call to the required variables to parameters.
return function_name ( Parameters );
}
}
这是我到目前为止的进展,我一直收到这个错误"Assignment makes pointer from integer without a cast" 如果有人更正我的代码并告诉我我的缺陷在哪里,我将不胜感激。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int vowels(char *str[],int len)
{
int i,s=0;
if (!str || len<1)
return s;
for(i=0;i<len;i++){
if (str[i]='a') || (str[i]='e') || (str[i]='i') || (str[i]='o') || (str[i]='u')
|| (str[i]='A') || (str[i]='E') || (str[i]='I') || (str[i]='O') || (str[i]='U'){
return s++;
}
}
return s;
}
int main (void)
{
int i,len;
int pom;
char *str[10];
len=strlen(*str);
for(i=0;i<len;i++){
scanf("%s",str[i]);
pom=vowels(str,len);
printf("%d",pom);
}
return 0;
}
if (str[i]='a')
应该是
if (str[i]=='a')
等等...
还有:
return s++;
应该是
s++;
以防递归很重要(注意签名更改):
int vowels(char *str,int len)
{
if (!str || len<1)
return 0;
if ((*str=='a') || (*str=='e') || (*str=='i') || (*str=='o') || (*str=='u') || (*str=='A') || (*str=='E') || (*str=='I') || (*str=='O') || (*str=='U'))
return 1 + vowels( str+1, len-1 );
else
return vowels( str+1, len-1 );
}
更新:因为这个答案被接受但它没有完全回答问题(它不包含函数vowels()
)我添加了我的递归版本函数 vowels()
:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* Return the number of vowels in a string.
*
* The characters treated as vowels are:
* 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'.
* The function checks the first character of the input string
* then calls itself recursively for the rest of the string.
*
* @param string str the string to check for vowels
* @return int the number of wovels found in the string
*/
int vowels(char *str)
{
/* The number of vowels found in the string so far */
int count = 0;
/* Recursion stop condition: when the string is empty */
if (! str[0]) {
/* An empty string does not contain any vowels */
return count;
}
/* Check the first character of the string */
if (strchr("aeiouAEIOU", str[0])) {
/* Found a vowel, count it */
count ++;
}
/* Recursive call to find the number of vowels after the first character */
return count + vowels(&str[1]);
}
int main(void)
{
int i, len;
int pom;
char *str[10];
int count = sizeof(str)/sizeof(str[0]);
for (i = 0; i < count; i ++) {
/* Hopefully you will not input strings longer than 254 characters */
str[i] = (char *)malloc(255);
fgets(str[i], 255, stdin);
pom = vowels(str[i]);
printf("%d\n", pom);
/* Release the memory */
free(str[i]);
}
return 0;
}
#include <stdio.h>
#include <ctype.h>
int vowels(char *str, int count){
if (!*str)
return count;
char ch = tolower(*str);
int match = ch=='e' || ch=='a' || ch=='i' || ch=='o' || ch=='u';
return vowels(++str, count + match);
}
int main (void){
int i, t, pom;
char str[32];
scanf("%d", &t);
for(i=0;i<t;i++){
scanf("%31s",str);
pom=vowels(str, 0);
printf("%d\n",pom);
}
return 0;
}
如何编写递归函数
Type function_name ( Parameter declarations ) {
if( Termination condition of recursion ) {
return [ End value ];
}
else {
Calculation for the next call;
//Tail recursion call optimization can be expected by the simple call to the required variables to parameters.
return function_name ( Parameters );
}
}