Во время интервью меня попросили реализовать решение следующей задачи на языке программирования C:
Учитывая входную строку с фиксированным шаблоном содержимого, разделенным разделителем, мне нужно извлечь каждое конкретное содержимое.
Шаблон входной строки: начальное_сообщение|целое_значение_1|целое_значение_2|код_символа|конечное_сообщение
Ожидаемый результат:
Starting message: starting_message
Value 1 : integer_value_1
Value 2 : integer_value_2
Char code : character_code
Ending message : ending_message
Пример ввода: HelloWorld|35|45|C|ByeWorld
Пример вывода:
Starting message: HelloWorld
Value 1 : 35
Value 2 : 45
Char code : C
Ending message : ByeWorld
Я реализовал следующий код:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/*
Loop through the input string until termination.
Figure out the position of delimiters first. It will help in parsing later.
For string, count the number of chars and use memcpy for that number of elements.
To parse char to int, try atoi.
*/
void parse(const char* input, char* starting_message, int* value1, int* value2, char* char_code, char* ending_message)
{
int ctr = 0, pos_ctr=0;
int delim_pos[4]; /* To store the location of delimiters aka '|' */
char* value1_str = (char*) malloc(10);
char* value2_str = (char*) malloc(10);
while(input[ctr]!= '\0')
{
if(input[ctr] == '|')
{
if(pos_ctr < 4)
{
delim_pos[pos_ctr] = ctr;
pos_ctr++;
}
}
ctr++;
}
memcpy(starting_message,input,(delim_pos[0])); /* starting_message is contained in input string in between input[0] & input[delim_pos[0]]*/
starting_message[delim_pos[0]+1] = '\0';
memcpy(value1_str, input,(delim_pos[1]-delim_pos[0])); /* value1 is contained in input string in between input[delim_pos[0]] & input[delim_pos[1]]*/
value1_str[(delim_pos[1]-delim_pos[0] + 1)] = '\0';
*value1 = atoi(value1_str);
memcpy(value2_str, input,(delim_pos[2]-delim_pos[1])); /* value2 is contained in input string in between input[delim_pos[1]] & input[delim_pos[2]]*/
value1_str[(delim_pos[2]-delim_pos[1] + 1)] = '\0';
*value2 = atoi(value2_str);
*char_code = input[(delim_pos[2]+1)]; /* char_code is element next to input[delim_pos[2]]*/
memcpy(ending_message, input, (delim_pos[3]-ctr-1)); /* ending_message is contained in input string in between input[delim_pos[3]] & null termination char*/
ending_message[delim_pos[3]-ctr] = '\0';
}
int main() {
const char* input = "HelloWorld|35|45|C|ByeWorld";
char* starting_message = (char*) malloc(30);
char* ending_message = (char*) malloc(30);
int value1, value2;
char char_code;
parse(input, starting_message, &value1, &value2, &char_code, ending_message);
printf(" Input string: %s\n",input);
printf("Starting message : %s\n", starting_message);
printf("Value 1 : %d\n", value1);
printf("Value 2 : %d\n", value2);
printf("Character code : %c\n", char_code);
printf("Ending message: %s\n", ending_message);
return 0;
}
Я смог скомпилировать, но на экране вывода я столкнулся с ошибкой сегментации без вывода.
Где я ошибся и как это исправить?
atoi
. Его можно вызвать непосредственно во входной строке (плюс смещение до первого символа значения). Например.*value1 = atoi(&input[delim_pos[0]+1]);
. - person Ian Abbott   schedule 02.03.2021memcpy
копируются с начала входной строки. - person Ian Abbott   schedule 02.03.2021