簡體   English   中英

將字符轉換為位於argv [1]中字符串內的int

[英]Casting a character into an int which is located within a string within argv[1]

我試圖在存儲在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行。

我已嘗試根據您的Linux系統修改您的代碼,並根據您提供(或未提供)的數據在此處進行了少量修改,並在代碼中進行了修改。

我希望這可以幫助您了解代碼中的錯誤。

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

#define string char*
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();
    ptext = "Hello World";
}
else
return 1;

//this is to index the letters within the code word
int c = 0;
int d;
for (c = 0, d = strlen(b); c < d; c++)
{
//int k = (b(char[c]));
int k = *(b+c);
    //this is to index the letters within the plaintext
    int i = 0;
    int l;
    for (i = 0, l = strlen(ptext); i < l ;i++)
    {
        //This will cause buffer overflow, so commenting
        /*
        while (isalpha(ptext[i]))
        {
        printf("%c", b[c]%d+k);  //what are you trying to print as %d here
        }
        */
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM