0

I'm trying to cast a character within a string which is stored in argv[1] and an int variable named k that will have its corresponding ASCII value.

Furthermore, this character which I'm casting will also be continuously changing by the means of a for loop controlled by another int variable named c. I am posting the code below.

Let me know, what I'm doing wrong because I keep getting many different error messages.

#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);
        }              
    }    
  }           
}

The part in question is 11 lines from the bottom .

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
TeachMe
  • 155
  • 2
  • 3
  • 11
  • 3
    **What** error messages? – Oliver Charlesworth Jul 24 '15 at 02:46
  • 1
    Also, what is `string`? – Oliver Charlesworth Jul 24 '15 at 02:46
  • A string is an array of characters. GetString is a function from the library, sorry for not including it in the code. The error messages i get vary. For the code as it is right now, the error message is "expected expression" with a little pointer pointing at the c of the char – TeachMe Jul 24 '15 at 02:50
  • 2
    @OliverCharlesworth apparently this "cs50" thing includes `typedef char *string;` . Not confusing , not at all... – M.M Jul 24 '15 at 02:51
  • `int k = (b(char[c]));` should read `int k = b[c] - '0';`, but `b[c]%d+k` later on is incorrect, you'll need to rethink that one. Also , `isalpha(b)` is illegal, you should have got compiler messages about that. – M.M Jul 24 '15 at 02:53
  • @MattMcNabb sorry, what is typedef char *string; ? – TeachMe Jul 24 '15 at 02:53
  • What does the -0 in int k = b[c] - "0"; mean? Im not familiar with anything like that. – TeachMe Jul 24 '15 at 02:55
  • @EthanT [see here](http://stackoverflow.com/questions/781668/char-to-int-conversion-in-c). If I understand what you are trying to do; you want to change the value of the character, not perform a type conversion. `char` is an integer type in C. – M.M Jul 24 '15 at 03:02
  • I read the linked forum but I'm still not sure why the complier understands what you have done. Is there a reason why what I did was incorrect? Also, just to clarify, all I'm trying to do is to take an character which has position x within the argv[1] array and store its corresponding ASCII value in the int variable I have declared as k. – TeachMe Jul 24 '15 at 03:10
  • @ethan, which language you want to use here `C` or `C++`? – Pawan Jul 24 '15 at 04:58
  • If you are sure that the char is always from char ASCII '0' to ASCII '9' you could use aritmetic to solve the problem, as @MattMcNabb show you. In other words `int k = b[c] - '0';` is `b[c] - 0x30`. ASCII numbers start from 0x30 to 0x39. Obviously this type of convertions works with a single digit only. – LPs Jul 24 '15 at 06:03
  • yes, your code was incorrect: `b(char[c])` is a syntax error. The `[ ]` operator must have two objects as operands, not the name of a type. Aside from `b[c]`, there's no other combination of `b` and `c` that makes sense. – M.M Jul 24 '15 at 06:05
  • "*A string is an array of characters.*" In the context of the code you show `string` most likely is not an array of `char`, but a pointer to `char`, that is `char *`. – alk Jul 24 '15 at 06:18

1 Answers1

0

I've tried modifying your code as per my Linux system and posting here with few modification here and there in the code, based on data you provided (or rather didn't provided).

I hope this can help in you understanding what is wrong in your code.

#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
        }
        */
    }
}
}
Pawan
  • 1,537
  • 1
  • 15
  • 19