- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个程序,该程序接受语法不正确的文本(长度低于 990 个字符)作为输入,对其进行更正,然后返回更正后的文本作为输出。我尝试使用在线编译器“ideone”运行该程序,但它返回了很多我不太明白的错误。我已经发布了我的代码以及下面的错误图片。谁能向我解释这些错误到底意味着什么?
#include "stdio.h"
char capitalize(int i); //prototype for capitalize method
int main(void)
{
char userInput[1200]; //Array of chars to store user input. Initialized to 1200 to negate the possibility of added characters filling up the array.
int i; //Used as a counter for the for loop below.
int j; //Used as a counter for the second for loop within the first for loop below.
int numArrayElements;
printf("Enter your paragraphs: ");
scanf("%c", &userInput); //%c used since chars are expected as input(?)
numArrayElements = sizeof(userInput) / sizeof(userInput[0]); //stores the number of elements in the array into numArrayElements.
if (userInput[0] >= 97 && userInput[0] <= 122) //Checks the char in index 0 to see if its ascii value is equal to that of a lowercase letter. If it is, it is capitalized.
userInput[0] = capitalize(userInput[0]);
//code used to correct input should go here.
for (i = 1; i < numArrayElements; i++) //i is set to 1 here because index 0 is taken care of by the if statement above this loop
{
if (userInput[i] == 32) //checks to see if the char at index i has the ascii value of a space.
if (userInput[i + 1] == 32 && userInput[i - 1] != 46) //checks the char at index i + 1 to see if it has the ascii value of a space, as well as the char at index i - 1 to see if it is any char other than a period. The latter condition is there to prevent a period from being added if one is already present.
{
for (j = numArrayElements - 1; j > (i - 1); j--) //If the three conditions above are satisfied, all characters in the array at location i and onwards are shifted one index to the right. A period is then placed within index i.
userInput[j + 1] = userInput[j];
userInput[i] = 46; //places a period into index i.
numArrayElements++; //increments numArrayElements to reflect the addition of a period to the array.
if (userInput[i + 3] >= 97 && userInput[i + 3] <= 122) //additionally, the char at index i + 3 is examined to see if it is capitalized or not.
userInput[i + 3] = capitalize(userInput[i + 3]);
}
}
printf("%c\n", userInput); //%c used since chars are being displayed as output.
return 0;
}
char capitalize(char c)
{
return (c - 32); //subtracting 32 from a lowercase char should result in it gaining the ascii value of its capitalized form.
}
最佳答案
您的代码有几个问题,对于初学者来说非常典型。您上一条评论中问题的答案在于 scanf()
的工作方式:它将whitepsaces之间的所有内容作为 token ,因此它在 hey
之后结束。我对我发现的其余问题的代码进行了评论,但没有太挑剔。如果这篇文章下面的评论是这样的话,他们可能会这么做。
#include "stdlib.h"
#include "stdio.h"
#include <string.h>
// Check for ASCII (spot-checks only).
// It will not work for encodings that are very close to ASCII but do not earn the
// idiomatic cigar for it but will fail for e.g.: EBCDIC
// (No check for '9' because non-consecutive digits are forbidden by the C-standard)
#if ('0' != 0x30) || ('a' != 0x61) || ('z' != 0x7a) || ('A' != 0x41) || ('Z' != 0x5a)
#error "Non-ASCII input encoding found, please change code below accordingly."
#endif
#define ARRAY_LENGTH 1200
// please put comments on top, not everyone has a 4k monitor
//prototype for capitalize method
char capitalize(char i);
int main(void)
{
//Array of chars to store user input.
// Initialized to 1200 to negate the possibility of
// added characters filling up the array.
// added one for the trailing NUL
char userInput[ARRAY_LENGTH + 1];
// No need to comment counters, some things can be considered obvious
// as are ints called "i", "j", "k" and so on.
int i, j;
int numArrayElements;
// for returns
int res;
printf("Enter your paragraphs: ");
// check returns. Always check returns!
// (there are exceptions if you know what you are doing
// or if failure is unlikely under normal circumstances (e.g.: printf()))
// scanf() will read everything that is not a newline up to 1200 characters
res = scanf("%1200[^\n]", userInput);
if (res != 1) {
fprintf(stderr, "Something went wrong with scanf() \n");
exit(EXIT_FAILURE);
}
// you have a string, so use strlen()
// numArrayElements = sizeof(userInput) / sizeof(userInput[0]);
// the return type of strlen() is size_t, hence the cast
numArrayElements = (int) strlen(userInput);
// Checks the char in index 0 to see if its ascii value is equal
// to that of a lowercase letter. If it is, it is capitalized.
// Do yourself a favor and use curly brackets even if you
// theoretically do not need them. The single exception being "else if"
// constructs where it looks more odd if you *do* place the curly bracket
// between "else" and "if"
// don't use the numerical value here, use the character itself
// Has the advantage that no comment is needed.
// But you still assume ASCII or at least an encoding where the characters
// are encoded in a consecutive, gap-less way
if (userInput[0] >= 'a' && userInput[0] <= 'z') {
userInput[0] = capitalize(userInput[0]);
}
// i is set to 1 here because index 0 is taken care of by the
// if statement above this loop
for (i = 1; i < numArrayElements; i++) {
// checks to see if the char at index i has the ascii value of a space.
if (userInput[i] == ' ') {
// checks the char at index i + 1 to see if it has the ascii
// value of a space, as well as the char at index i - 1 to see
// if it is any char other than a period. The latter condition
// is there to prevent a period from being added if one is already present.
if (userInput[i + 1] == ' ' && userInput[i - 1] != '.') {
// If the three conditions above are satisfied, all characters
// in the array at location i and onwards are shifted one index
// to the right. A period is then placed within index i.
// you need to include the NUL at the end, too
for (j = numArrayElements; j > (i - 1); j--) {
userInput[j + 1] = userInput[j];
}
//places a period into index i.
userInput[i] = '.';
// increments numArrayElements to reflect the addition
// of a period to the array.
// numArrayElements might be out of bounds afterwards, needs to be checked
numArrayElements++;
if (numArrayElements > ARRAY_LENGTH) {
fprintf(stderr, "numArrayElements %d out of bounds\n", numArrayElements);
exit(EXIT_FAILURE);
}
// additionally, the char at index i + 3 is examined to see
// if it is capitalized or not.
// The loop has the upper limit at numArrayElements
// i + 3 might be out of bounds, so check
if (i + 3 > ARRAY_LENGTH) {
fprintf(stderr, "(%d + 3) is out of bounds\n",i);
exit(EXIT_FAILURE);
}
if (userInput[i + 3] >= 97 && userInput[i + 3] <= 122) {
userInput[i + 3] = capitalize(userInput[i + 3]);
}
}
}
}
printf("%s\n", userInput);
return 0;
}
char capitalize(char c)
{
// subtracting 32 from a lowercase char should result
// in it gaining the ascii value of its capitalized form.
return (c - ' ');
}
关于C初学者: Need explanation of error messages from "ideone",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39981689/
我不明白为什么我会从 GHCi 得到以下回复。不是Maybe构造函数? Prelude> :t Maybe :1:1: Not in scope: data constructor `Maybe' P
场景是我在此网站上有不同的访问级别,我有一个适用于所有有效用户的简单登录流程,但是我现在尝试分隔不同的用户以实现对页面的不同访问。 这是我页面开头的代码: // CHECKS IF THE USER
我的任务是:写下数字1-100。如果该数字可以被 3 整除,则将其写入数字“它可以被 3 整除”旁边的控制台。如果数字是 5,也将其写入数字旁边的控制台“它可以被 5 整除”,如果它不能被 3 整除,
我有一堆实现协议(protocol) P 的记录 (A B C)。 我想编写一个方法,该方法将选择一种记录类型,构造它,然后调用它的方法。 例如,如果我有一个记录列表(def types '(A B
我的任务是编写一个程序,根据以下三个因素来预测您的年度燃料使用量汽车加油。我必须使用两个单独的类。这是我的第一个类,名为 AnnualFuelUse。 public class AnnualFuelU
我是 JavaScript 新手。我已经通过 Learning JavaScript (o'reilly) 完成了我的工作,但我只是想制作我的第一个 JavaScript。 我认为最好从事我感兴趣的事
我真的刚刚开始学习如何用 Python 编写代码。我有兴趣 如何重现 u[x,t] 矩阵。我尝试了 return u,它抛出了一个错误。 如果此代码中 for 循环的位置正确并正常运行。 最重要的是,
我不明白 view("") 在作为 Model 对象一部分的以下 javascript 方法中的作用: addView: function(view) { this.views
所以我正在尝试将 AppKit 导入到我的 python 项目中。我正在使用 pyCharm,但每次我尝试导入时,都会收到以下错误消息: You are using pip version 6.0.8
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 2 年前。 Improve
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
好吧,不知道在哪里问这个,但我是一个初学者程序员,使用 Perl。我需要创建一个数组数组,但我不确定使用数组/哈希引用、哈希数组或数组哈希等是否更好。 我需要一组匹配项:@totalmatches 每
我最近开始学习 PHP。我做了一个基本的网站,基本上想给它密码。如果有人能告诉我为什么这不起作用,我将不胜感激。我知道它不起作用,因为我已经尝试过了;我只是不明白为什么。 ... REST OF W
我试图理解 C 中的整个指针和取消引用。我几乎明白了,但遇到了非常简单的代码,结果我不明白: char *ptr = "Characters"; char val = *ptr; char *chrp
首先,我有这个列表(在练习中建议): Members = [('Tessa','G1'),('Evan','G2'),('Tom','G3'), ('Mia','G3'),('Claire','G3'
我有以下列表: listofanimals = ['Dog', 'Cat', 'Frog', 'Tiger', 'Sheep', 'Lion'] 我想根据字典对这个列表进行排序: score_card
1 userID = floatval($userID); 13 } else { 14 $this->userID = floatval(
我是 R 新手,遇到了一段我不理解的代码。更具体地说,我想知道 .Internal做。这是我尝试转换为 Matlab 的示例: dunif 我想知道.Internal和 做。 非常感谢您, 西蒙 最
我是一名优秀的程序员,十分优秀!