我被困在n0 v0w3ls exercise from CS50号公路上了.我想不出如何使用非空白输出.我完全搞不懂字符串/字符之间的数据类型转换,并将新值赋给输出字符.我try 了这么多组合,我再也不明白其中的区别了.现在,我的程序可以在调试器中运行,但输出为空.任何非常感谢的帮助;)
#include <cs50.h>
#include <stdio.h>
#include <string.h>
char replace(string input);
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Incorrect number of arguments\n");
return 1;
}
else
{
printf("%c \n", replace(argv[1]));
}
}
char replace(string input)
{
int n = strlen(input);
//n+1 as I read the last char should be a '\0' to be recognised as a string
char s[n+1];
for (int i = 0; i < n; i++)
{
//printf("%c\n", input[i]);
switch (input[i])
{
case 97:
s[i] = '6' ;
break;
case 101:
s[i] = '3';
break;
case 105:
s[i] = '1';
break;
case 111:
s[i] = '0';
break;
default:
s[i] = input[i];
break;
}
}
return s[n];
}
其目的是用数字代替元音.我试过了
- 将输入作为字符串
- 在该字符串的每个字符上循环
- 如果字符是元音>;在我的输出字符串中替换它,否则保持原样
- 是否返回我的输出字符数组/字符串?
// Write a function to replace vowels with numbers
// Get practice with strings
// Get practice with command line
// Get practice with switch
#include <cs50.h>
#include <stdio.h>
#include <string.h>
string replace(string input);
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Incorrect number of arguments\n");
return 1;
}
else
{
printf("%s \n", replace(argv[1]));
}
}
string replace(string input)
{
int n = strlen(input);
string s[n+1];
for (int i = 0; i < n; i++)
{
//printf("%c\n", input[i]);
switch (input[i])
{
case 'a':
s[i] = "6" ;
break;
case 'e':
s[i] = "3";
break;
case 'i':
s[i] = "1";
break;
case 'o':
s[i] = "0";
break;
default:
s[i] = &input[i];
break;
}
}
s[n+1] = "\0";
return s[n];
}