* Description : This program allows a user to enter a password and and the name of a text file. After the user
* has entered sufficient information, the program will encode the text and save the overwrite the file with the
* encoded message.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char make_lower (char ch){//This converts the text of the selected file to all be lowered
int i;//initializing
if (isalpha(ch)==1) ch=tolower(ch);//if the selected item in the file is an uppercase character, then it converts it to lowercase.
else;//other-wise do nothing
return ch;
}
void randomize (char arr[], int n, int pswd)//Function that randomizes the alphabet to use while encoding
{
int i;//initialing
char ch;//initializing
// Use a different seed value so that we don't get same
// result each time we run this program
srand (pswd);//using the seed the user has entered
// Start from the last element and swap one by one. We don't
// need to run for the first element that's why i > 0
for (i = 0; i < 26; ++i)//for loop the iterates through the alphabet array
{
// Pick a random index from 0 to 25
int random = rand()%26;
// Swap arr[i] with the element at random index
char tmp = arr[random];//swapping
arr[random] = arr[i];//swapping
arr[i] = tmp;//swapping
}}
char encode (char ch, char alpha[]){//function that encodes a message using the alphabet that was randomized
int i, tmp;//initializing
if (isalpha(ch)== 2){//if the selected character is a lowercase then it gets swapped with the "encoded" character.
int k = ch;
ch = alpha[k-97];
}
return ch;}
int main(int argc,char *argv[]) {
char alpha[26] = ("abcdefghijklmnopqrstuvwxyz");//initializing char array
randomize(alpha, 26, argc);
int i=0;
char ch;
while (argv[i] != EOF){
ch = argv[i];
ch = make_lower(ch);
ch = encode(ch,alpha);
putchar(ch);
++i;
}
}
Я не знаю, почему я продолжаю получать ошибку: ошибка сегментации (сбрасывание ядра). Я считаю, что у меня есть указатель на изгоев, но я не знаю, как это исправить. Я использовал отладчик и все еще не могу исправить проблему.Почему у меня ошибка сегментации?
Может кто-нибудь помочь? Спасибо!
'ch = argv [i];' является ошибкой. Вы должны получить сообщения компилятора об этом; обратите внимание на них! Я * думаю *, что вы на самом деле пытаетесь сделать, это петля над одной введенной строкой (вместо того, чтобы перебирать все строки), вы, вероятно, имели в виду 'while (argv [1] [i]! = '\ 0') {ch = argv [1] [i]; 'и т. д. –
Ваш код не имеет большого сходства с описанием. Вы не используете пароль, введенный пользователем (вместо этого вы используете 'argc', который является подсчетом количества аргументов).Вы не пытаетесь открыть файл или прочитать данные, которые должны быть закодированы, и в описании рандомизации ничего не упоминается. –