#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
//make sure only 2 command line arguments entered
if( argc != 2)
{
printf("Please input a keyword composing of letters only\n");
return 1;
}
else
{
// declare variable "m" to designate the number of elements in the array "keyword"
int m = strlen(argv[1]);
//array declaration for "keyword" with "m" elements
int keyword[m];
//convert characters to integers
keyword[m] = atoi(argv[1]);
//iterate through characters in argv[1] in order to printf the elements in the array
for (int j = 0; j < strlen(argv[1]); j++)
printf("%i",keyword[j]);
}
}
所以,我知道这确实是错的,但是有人能指出我正确的方向吗?
答案 0 :(得分:1)
我不确定我是否正确地理解了你想要做什么,但是如果你只想从你的程序参数中给出的数字中提取每一个构成它的数字,你就可以这样做
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char** argv)
{
//make sure only 2 command line arguments entered
if( argc != 2)
{
printf("Please input a keyword composing of letters only\n");
return 1;
}
else
{
// declare variable "m" to designate the number of elements in the array "keyword"
int m = strlen(argv[1]);
//array declaration for "keyword" with "m" elements
int keyword[m];
//convert characters to integers
//iterate through characters in argv[1] in order to printf the elements in the array
for (int j = 0; j < strlen(argv[1]); j++){
keyword[j] = argv[1][j] - '0';
printf("%d\n",keyword[j]);
}
}
}