将一个字符转换为一个int,该int位于argv [1]内的字符串中

时间:2015-07-24 02:45:01

标签: c cs50

我正在尝试在存储在argv[1]中的字符串和一个名为k的int变量中转换一个字符,该变量将具有相应的ASCII值。

此外,我正在投射的这个角色也将通过另一个名为c的int变量控制的for循环来不断变化。我发布下面的代码。

让我知道,我做错了,因为我不断收到许多不同的错误消息。

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, string argv[])
{
int a = argc;
if (a != 2)
{
    return 1;
}

string b = argv[1];
//b IS THE CODE WORD
string ptext;
//ptext IS THE MESSAGE TO BE ENCRYPTED

if (isalpha(b))
{
    ptext = GetString();
}
else
return 1;

//this is to index the letters within the code word       
for (int c = 0, d = strlen(b); c < d; c++)      
{ 
int k = (b(char[c]));     
    //this is to index the letters within the plaintext         
    for (int i = 0, l = strlen(ptext); i < l ;i++)
    {
        while (isalpha(ptext[i]))
        {
        printf("%c", b[c]%d+k);
        }              
    }    
  }           
}

有问题的部分是从底部开始的11行。

1 个答案:

答案 0 :(得分:0)

我已尝试按照我的Linux系统修改您的代码,并根据您提供的数据(或更确切地说未提供)在此处发布,并在代码中进行少量修改。

我希望这有助于您了解代码中的错误。

source