0

While working on Caesar i realized i needed a function to convert an int to a string and searched online. The first one I came across was the itoa function but it turned out it wasn't a standard function so i tried using sprintf() instead but still it was not working. I then tried to implement itoa myself by creating it as a custom function in my program by copying it (not sure if this violates academic honesty will change it if it does) however I keep getting the following error-

tmp/caesar-e9b6f3.o: In function `reverse':
/home/ubuntu/pset2/caesar/caesar.c:62: undefined reference to `swap'
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
<builtin>: recipe for target 'caesar' failed
make: *** [caesar] Error 1

Here is my code -

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
char* itoa(int value, char* buffer, int base);
int main (int argc, string argv[])
{
    int j;
    int n = strlen(argv[1]);
    string cyphertext[n];
    string s[n];
    if (argc == 2)
    {
        for (int i = 0; i < n ; i++)
    if (isdigit(argv[1][i]))
    {
        string plaintext = get_string("plaintext: ");
        
        printf("%d \n", atoi(argv[1]));
        for (j = 0; j < n ; j++)
       if (isupper(plaintext[j]))
        {
            int  letter = plaintext[j];
             itoa(65 + (letter + atoi(argv[1]))%26 , cyphertext[j], 10);
        }
       else if (islower(plaintext[j]))
        {
            int  letter = plaintext[j];
            itoa(65 + (letter + atoi(argv[1]))%26 , cyphertext[j], 10);
        }
        else 
        {
            ;
        }
        printf("%s", cyphertext[2]);
        string cyptext[n];
        
        }
        return 0;
    }
    
    {
        printf("Usage: ./caesar key");
        return 1;
    }

    }
    

char* itoa(int value, char* buffer, int base);

// inline function to swap two numbers
inline void swap(char *x, char *y) {
    char t = *x; *x = *y; *y = t;
}

// function to reverse buffer[i..j]
char* reverse(char *buffer, int i, int j)

    {while (i < j)
        swap(&buffer[i++], &buffer[j--]);

    return buffer;}


// Iterative function to implement itoa() function in C
char* itoa(int value, char* buffer, int base)
{
    // invalid input
    if (base < 2 || base > 32)
        return buffer;

    // consider absolute value of number
    int n = abs(value);

    int i = 0;
    while (n)
    {
        int r = n % base;

        if (r >= 10) 
            buffer[i++] = 65 + (r - 10);
        else
            buffer[i++] = 48 + r;

        n = n / base;
    }

    // if number is 0
    if (i == 0)
        buffer[i++] = '0';

    // If base is 10 and value is negative, the resulting string 
    // is preceded with a minus sign (-)
    // With any other base, value is always considered unsigned
    if (value < 0 && base == 10)
        buffer[i++] = '-';

    buffer[i] = '\0'; // null terminate string

    // reverse the string and return it
    return reverse(buffer, 0, i - 1);
}

I haven't yet written the loop for the output string i will do that once the function starts working

1

There is no need for itoa function. If you make a separate function for doing the cypher which returns a integer and assigns it to a char variable then the integer is converted to its string equivalent.

char c;
c = cypher(text, key);

int cypher(char text, char key)
{
   int result;
   result = (65 + (key + atoi(text)%26);
   return result;
}

The function returns an int and gives it to a char. The int is converted to equivalent ascii character

If this helps then please check the tickmark.

2
  • i tried to do that can you add a few lines of code showing an example? I wrote something like this - cyptext[j] = (char) ((65 + (letter + atoi(argv[1]))%26) :) Oct 7 '20 at 15:14
  • thanks a lot :) Oct 7 '20 at 17:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .