I get segmentation fault at this point in my code and I can't figure out why
string convert(string text, int num_copy)
{
string new_index = NULL, answer;
for (int j = 0; ; j ++)
{
if (isupper(text[j]))
{
new_index[j] = text[j] - 65;
}
else if (islower(text[j]))
{
new_index[j] = text[j] - 97;
}
else
{
new_index[j] = text[j];
}
}
for (int x = 0; x < num_copy; x ++)
{
string cipher;
cipher[x] = new_index[x] + num_copy;
printf("%c\n", cipher[x]);
answer = cipher;
}
return answer;
}
this is the whole code
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
string convert(string text, int num_copy);
bool number(int num, string copy);
int main(int argc, string argv[])
{
string text;
//string text = get_string("Plaintext: ");
string copy = argv[1];
int num = atoi(copy);
int num_copy = num;
if (argc != 2)//if argc != 2, program will not run
{
printf("Usage: ./caesar key\n");
return 1;
}
else
{
if (number(num, copy))
{
printf("yes\n");
}
}
text = get_string("Plaintext: "); //plaintext by user
for (int i = 0; ; i ++)
{
if (isalpha(text[i]))
{
//if isupper then leave it as upper
//if islower then leave it as lower
convert(text, num);
}
else
{
//leave it as it is
}
}
//create a function for caesar
}
string convert(string text, int num_copy)
{
string new_index = NULL, answer;
for (int j = 0; ; j ++)
{
if (isupper(text[j]))
{
new_index[j] = text[j] - 65;
}
else if (islower(text[j]))
{
new_index[j] = text[j] - 97;
}
else
{
new_index[j] = text[j];
}
}
for (int x = 0; x < num_copy; x ++)
{
string cipher;
cipher[x] = new_index[x] + num_copy;
printf("%c\n", cipher[x]);
answer = cipher;
}
return answer;
}
bool number(int num, string copy)//function to check if argv is a number
{
for (int q = 0; q != '\0';q ++)
{
if (isdigit(copy[q]) && (num > 0))//if the key is numeric
{
return true;
}
else//if key is non-numeric
{
printf("Usage: ./caesar key\n");
return false;
}
}
return true;
}