- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要为学校制作这个程序,但是由于“段错误”(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);
}
最佳答案
在函数 replaceVowelsA()
中,您将输入的字符串替换为特定字符。在函数 replaceVowelsA()
中,您使用 findOcurrace()
查找 Char
是否存在。如果不是则返回-1,但是你没有检查返回值,所以它可能返回-1,在第22行代码中,它可能执行代码str[-1] = 'a'
,这是一种非常危险的行为。
您的字符串操作还有其他问题,例如您没有检查的终止字符 NULL
。
如果你使用 gnu 工具链,你可以添加 -g
选项。您也可以使用其他调试方法来查找问题。
关于c - 学校项目分割错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39724574/
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
我是一名二年级的 ICT 学生。今年之前我从未接触过 PHP,我们的讲师给了我们基础知识,并在学期末给了我们一个项目,该项目将结合我们在他的类(class)和数据库类(class)中学到的知识。我们要
对于 JS 来说,我确实是个新手,而且我从学校收到了一个我无法理解的问题。我已经了解了一些,但非常感谢一些提示/帮助,因为我完全陷入困境! 它或多或少如下: 使用 HTML5 呈现一个网页,要求用户输
我对编程语言、django 和数据库模型相当陌生。所以我的问题很简单我在 models.py 中有 3 个模型 class UserProfileInfo(models.Model): # creat
是的,我知道 W3school,你们都讨厌它。我有时也会这样做,但现在它对我有帮助。如何为他们的网站制作删除 cookie 按钮?他们网站上的 cookie 示例: function setCo
我是一名优秀的程序员,十分优秀!