학교용으로이 프로그램을 만들어야하지만 "세분화 오류"(11)로 인해 오류 프로젝트가 종료됩니다. 나는 이것이 프로그램 동안 메모리의 부족과 관련이 있다고 생각하지만, 실제로 메모리가 극한의 양으로 사용되는 기능을 보지 못한다. 어떤 도움을 주시면 감사하겠습니다.학교 프로젝트 세분화 오류
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define _CRT_SECURE_NO_WARNINGS
int STRLEN = 36;
int findOcurrance(char str[], char Char1){
for(int i = 0; i < STRLEN * 3; i++){
if(str[i] == Char1)
return i;
}
return -1;
}
void replaceVowelsA(char str[], char Char) {
for(int i = 0; i < STRLEN * 3; i++) {
int index = findOcurrance(str, Char);
str[index] = 'a';
}
}
void insertaChar(char str[], char Char1, int index){
for(int i = STRLEN * 3; i >= index; i--){
if(i != 0 && (str[i] != '\0' || i == strlen(str))){
str[i] = str[i - 1];
}
else if(i == 0){
str[0] = ' ';
}
}
str[index] = Char1;
}
void adday(char str[]) {
char consonants[42] = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'};
for(int i = 0; i < STRLEN * 3; i++){
if(i==0){
for(int c = 0; c < 42; c++){
if(str[i] == consonants[c]){
insertaChar(str, 'y', i);
insertaChar(str, 'a', i);
break;
}
}
}
else if(i != 0){
if(str[i-1] == ' '){
for(int c = 0; c < 42; c++){
if(str[i] == consonants[c]){
insertaChar(str, 'y', i);
insertaChar(str, 'a', i);
break;
}
}
}
}
}
}
int findWords(char str[]){
int count = 0;
for(int i = 0; i < STRLEN * 3; i++){
char c = str[i];
if(isspace(c))
count++;
if(count == 2)
return i + 1;
}
return -1;
}
char* stringReorder(char str[], int i){
if(i == -1){
return str;
}
else{
char string1[STRLEN];
char string2[STRLEN * 2];
strncpy(string1, str, i);
strncpy(string2, &str[i], strlen(str) - i);
char string[STRLEN * 3];
strcpy(str, string2);
strcat(str, " ");
strcat(str, string1);
return str;
}
}
void main(void) {
char mystring[STRLEN * 3];
printf("** Welcome to the Double Dutch game **\n");
printf("Please enter a string: ");
scanf("%[^\n]s", mystring);
char vowel = 'e';
replaceVowelsA(mystring, vowel);
vowel = 'i';
replaceVowelsA(mystring, vowel);
vowel = 'u';
replaceVowelsA(mystring, vowel);
vowel = 'o';
replaceVowelsA(mystring, vowel);
adday(mystring);
int index = findWords(mystring);
strcpy(mystring, stringReorder(mystring, index));
printf("Double Dutch translation: %s", mystring);
}
시작하려면 어디에서나 문자열 종결자를 확인하지 않는 것. –
코드를 작성하는 방법을 배우는 것보다 디버거를 사용하는 방법을 배우는 것이 더 중요합니다. 이제는 이전 스킬을 배울 때입니다. –
'strncpy()'는 이름에도 불구하고 ** string ** 함수가 아닙니다. 결과 문자열은 모든 경우에 제로 - 종료되지 않을 수도 있습니다. 적절한 문자열 함수 (예 :'strcat()')에 결과 배열을 사용할 수 없습니다. – pmg