0

When i try to run this code, it gives me segmentation fault. Couldn't figure out. Also i know it seems a bit complex writing because im learning from zero. Hope you guys will appreciate that.

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

int main(int argc, char* argv[])
{
    string key = argv[1];
    int key_l = strlen(key);

    for (int j = 0; j < key_l; j++) //checking key arrays 
    {
        if (argc != 2) //accept only if there is 1 word input
        {
            printf("please insert only 1 word!.\n");
            return 1;
        } 
        else if (isalpha(key[j])) // if input is alphabet characters
        {
            string input = GetString(); //get input from user
            int input_l = strlen(input);

            for (int i = 0; i < input_l; i++)
            {
                int key_i = atoi(key); // convert alphabetical key to number
                if (isalpha(input)) // check input is alphabet
                {
                    int output;
                    if (islower(input)) //if array is lower 
                    {
                        key_i = key_i % 97; /* "a" starts with 97 in Ascii so if i use mod 97,
                        i can get incremantal value*/
                        output = input[i] + key_i;
                        if (output > 122)
                        {
                            output = output - 65; //if value bigger than 90 it ll be different symbol.
                            printf("%c", output);
                        }
                        else
                        {
                            printf("%c", output);
                        }
                    }
                    if (isupper(input)) // if array is upper
                    {
                         key_i = key_i % 65; /* "A" starts with 65 in Ascii so if i use mod 65,
                        i can get incremantal value*/
                        output = input[i] + key_i;
                        if (output > 90)
                        {
                            output = output - 65;
                            printf("%c", output);
                        }
                        else
                        {
                            printf("%c", output);
                        }
                    }
                    else
                    {
                        printf(" \n");
                    }
                }
                else //if character not alphabet
                {
                    printf("please use alphabetical characters!.\n");
                    return 1;
                }
            }
        }
        else
        {
            printf("input alphabet key pls!\n");
        }
    }
}
1
         if (isalpha(input))

the isalpha() function takes a single character as input. You're trying to feed it an entire string, so it chokes and throws a seg fault. The same can be said for isupper() and islower().

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

0

else if (isalpha(key[j])) key[j] is a string while isalpha() takes a single character as an input. It's better to change into isalpha(argv[1][j]) or isalpha(key[1][j])

Similarly, (islower(input)) islower() function only receives char type as an input but your input is a string. So you should try to get each character of that string before using islower() My suggested solution:

for (i = 0, n = strlen(input);i<n;i++) { isalpha(input[i]); //get each character islower(input[i]); } Hope it helps :)

You must log in to answer this question.

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