0

I have been stumbling over vigenere problem from pset2. I am trying to iterate wrapping around text, compiler systematically yells at me.

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


int main(int argc, char* argv[])
{
    //checking if user have provided key. If not, inform user and exit
    if (argc != 2)
    {
        printf("Usage: ./vigenere <key>\n");
        return 1;
    }

    char* key = argv[1];

    if (!isalpha(key[0]))
    {
        printf("Please provide a string\n");
        return 1;
    }


    char* text = GetString();


    for(int i = 0, n = strlen(&text[i]); i < n; i++)
    {   
        //strlen(argv[1]) is constant
        if(strlen(argv[1]) < strlen(text))
            {
                //find out how to apply result
                int m = strlen(&text[i]) % strlen(argv[1]);
                printf("strlen(&key[i]) %i strlen(&text[i]):%i  ~(text):%i m:%i strlen(argv[1]):%i text:%c textint:%i",strlen(&key[i]), 
                strlen(&text[i]),strlen(text), m, strlen(argv[1]),text[i], (int)text[i]); 
                printf("\n");

            }

        else
        {

        } 

        //Non-alphabetical characters remain unchanged
        if(!isalpha(text[i]))
        {
            int cipher = text[i];
            printf("%c", (char) cipher);
        }

        if(isupper(text[i]))
        {

            if(text[i] + key[i] > 156)
            {
            //finish it 
                int cipher = (text[i] + key[i]);
                printf("%c", (char) cipher);
            }
            else
            {
                int cipher = (text[i] + key[i] - 'A');
                printf("%c", (char) cipher);
            } 

        }

        if(islower(text[i]))
        {
            if(text[i] + key[i] >= 220)
            {
            //fix the algorithm(hint: exceeds boundary after 220)
                int cipher = (text[i] + key[i])%26 + 97  ;
                printf("%c", (char) cipher);
            }
            else
            {
                int cipher = (text[i] + key[i] - 'a');
                printf("%c", (char) cipher);
            }


            /*int cipher = (((text[i] + key[i]) %26) + 97);
                printf("%i\n", cipher);
                printf("text[i]: %i key[i]:%i",text[i],key[i]);
                printf("\n");
                printf("%c\n", (char) cipher);
                printf("text[i]: %c key[i]:%c",(char) text[i],(char) key[i]);
            */
        }

    }
    printf("\n");


}

Ignore comments, print statements and part of toupper & tolower part, I havent modified it yet. My question is how can I assign modulo's value to the text?

2
  • There are a number of issues in the posted code, but you have said that you're still working on a lot of it, so my question is this: Exactly what are you trying to figure out? Can you please be more specific and give details? Also, can you show what you have tried? I wasn't able to see what you had attempted, and couldn't determine where you think you should.
    – Cliff B
    Sep 9 '15 at 6:32
  • I am trying to figure out how to wrap key around the the text. Drama starts inside for loop in the if statement.
    – user9453
    Sep 9 '15 at 7:59
0

Your basic misunderstanding, is in the usage of the strlen(). When using strings, the name of the variable where the string is stored, is also the address of the whole string. So say we have

char* str = "Hello"

The pointer to str is going to be str itself, not &str as you have used. And especially not &str[0] or whatever. The & reference operator returns to you the pointer pointing to a variable. But it's not usable to ask for the address of pointer.

So change all

strlen(&text[i])

to just

strlen(text)

to find the length in characters of text. If you are facing any other problem please be more specific.


If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

You must log in to answer this question.