如何在 C 中读取 space 之后的下一个字符?
How to read the next character after space in C?
所以我必须编写一个程序,输入是:
Spring Miles Indie Lie Ego
结果将是:
微笑
但是我的输入有一些问题。我不知道输入的长度,所以它可以是任何长度。
这就是我使用 scanf 读取每个字符的原因。我计算 spaces 以了解我要保存我需要的字符的数组的大小。
所以在这些之后这是我的主要问题:
如果我找到 space 我应该保存我从 scanf 读取的下一个字符,但我该怎么做呢?
或者你们知道解决这个问题的其他方法吗?
到目前为止,这是我的代码:
int main()
{
char ch;
int i = 0, count= 0;
while(scanf("%c", &ch) != EOF)
{
if(ch == '\n')
count = 0;
if(ch == ' ')
count++;
char array[count+1];
if(ch == ' ')
array[i++] = ch + 1; // I tried this, but it doesn't work.
}
return 0;
}
ch
是一个 char
。这里把数字1
加到字符"space"上是没有意义的。相反,您必须从字符串中读取下一个字符。你可以这样做:
int main()
{
char ch;
bool had_space;
int i = 0, count= 0;
had_space = true;
while(scanf("%c", &ch) != EOF)
{
if(ch == '\n')
count = 0;
if(ch == ' ')
count++;
char array[count+1];
if(ch == ' ')
had_space = true;
else if (had_space)
array[i++] = ch;
}
return 0;
}
我猜你还必须在循环外定义数组。
如果你在while循环中定义char数组,这个数组永远不会available.If while循环运行 10次,你的代码会定义10个char数组,但是有一个在while循环之后是可用的while 循环 done.Bellow 是我的代码
int main() {
char ch;
char array[1024];
int i = 0, count= 0;
int status = 1;
while(scanf("%c", &ch) != EOF) {
if(ch == '\n') {
break;
}
if(ch == ' ') {
status = 1;
continue;
}
if (status == 1) {
status = 0;
array[i++] = ch;
}
}
array[i++] = ' ';
printf("%s", array);
return 0;
}
我的代码是基于数组长度不会超过长度的假设 1024.We 在别人输入之前无法知道输入的长度 something.So malloc 可能对你有帮助。
Malloc 是 C.You 中的一个函数,可以用它来管理您的主内存。
如果您的输入一开始确实未知,并且您不知道它将包含多少空格,则您需要动态内存分配——这意味着您根据需要分配内存,而不是试图坚持下去在堆栈上固定大小的内存中。有几种方法,但我认为简单的链表是最干净的。以下应该满足您的需要:
typedef struct linkedListNode {
char ch;
struct linkedList * next;
} linkedListNode_t;
linkedListNode_t *pHead = NULL;
linkedListNode_t **ppNext = &pHead;
// pHead points to the first node in the list
// ppNext points to the pointer to the next element in the list
while(scanf("%c", &ch) != EOF) {
if (ch == ' ') {
// scan next character, and add to linked list:
if (scanf("%c", &ch) == EOF)
break; // exit loop if we've hit end of input
// ch is ok -- add to list:
// allocate a new pNode to store the information
linkedListNode_t *pNode = malloc(sizeof(linkedListNode_t);
if (pNode == NULL) {
printf("Out of memory -- aborting");
exit (-1); // if allocation failed, abort program
}
pNode->ch = ch;
pNode->next = NULL;
*ppNext = pNode; // make previous node point to this one.
ppNext = &pNode->next; // update ppNext
}
else if (ch == '\n') {
// special handling of newline -- did you want to print the
// string and dump memory here?
}
} // end wile loop
// at this point we have a linked list with all characters that
// follow spaces -- print it out:
linkedListNode *pNode = pHead;
while (pNode != NULL) {
printf("%c", pNode->ch);
pNode = pNode->next;
}
printf("\n");
// free list:
pNode = pHead;
linkedListNode *pNext;
while (pNode) {
pNext = pNode->pNext;
free(pNode);
pNode = pNext;
}
免责声明:我没有测试过上面的内容,所以我可能犯了语法错误。无论如何,您看起来对 C 有点陌生,所以我强烈建议您将其绘制出来以确保您了解它是如何工作的。
所以我必须编写一个程序,输入是:
Spring Miles Indie Lie Ego
结果将是: 微笑
但是我的输入有一些问题。我不知道输入的长度,所以它可以是任何长度。
这就是我使用 scanf 读取每个字符的原因。我计算 spaces 以了解我要保存我需要的字符的数组的大小。
所以在这些之后这是我的主要问题: 如果我找到 space 我应该保存我从 scanf 读取的下一个字符,但我该怎么做呢? 或者你们知道解决这个问题的其他方法吗?
到目前为止,这是我的代码:
int main()
{
char ch;
int i = 0, count= 0;
while(scanf("%c", &ch) != EOF)
{
if(ch == '\n')
count = 0;
if(ch == ' ')
count++;
char array[count+1];
if(ch == ' ')
array[i++] = ch + 1; // I tried this, but it doesn't work.
}
return 0;
}
ch
是一个 char
。这里把数字1
加到字符"space"上是没有意义的。相反,您必须从字符串中读取下一个字符。你可以这样做:
int main()
{
char ch;
bool had_space;
int i = 0, count= 0;
had_space = true;
while(scanf("%c", &ch) != EOF)
{
if(ch == '\n')
count = 0;
if(ch == ' ')
count++;
char array[count+1];
if(ch == ' ')
had_space = true;
else if (had_space)
array[i++] = ch;
}
return 0;
}
我猜你还必须在循环外定义数组。
如果你在while循环中定义char数组,这个数组永远不会available.If while循环运行 10次,你的代码会定义10个char数组,但是有一个在while循环之后是可用的while 循环 done.Bellow 是我的代码
int main() {
char ch;
char array[1024];
int i = 0, count= 0;
int status = 1;
while(scanf("%c", &ch) != EOF) {
if(ch == '\n') {
break;
}
if(ch == ' ') {
status = 1;
continue;
}
if (status == 1) {
status = 0;
array[i++] = ch;
}
}
array[i++] = ' ';
printf("%s", array);
return 0;
}
我的代码是基于数组长度不会超过长度的假设 1024.We 在别人输入之前无法知道输入的长度 something.So malloc 可能对你有帮助。 Malloc 是 C.You 中的一个函数,可以用它来管理您的主内存。
如果您的输入一开始确实未知,并且您不知道它将包含多少空格,则您需要动态内存分配——这意味着您根据需要分配内存,而不是试图坚持下去在堆栈上固定大小的内存中。有几种方法,但我认为简单的链表是最干净的。以下应该满足您的需要:
typedef struct linkedListNode {
char ch;
struct linkedList * next;
} linkedListNode_t;
linkedListNode_t *pHead = NULL;
linkedListNode_t **ppNext = &pHead;
// pHead points to the first node in the list
// ppNext points to the pointer to the next element in the list
while(scanf("%c", &ch) != EOF) {
if (ch == ' ') {
// scan next character, and add to linked list:
if (scanf("%c", &ch) == EOF)
break; // exit loop if we've hit end of input
// ch is ok -- add to list:
// allocate a new pNode to store the information
linkedListNode_t *pNode = malloc(sizeof(linkedListNode_t);
if (pNode == NULL) {
printf("Out of memory -- aborting");
exit (-1); // if allocation failed, abort program
}
pNode->ch = ch;
pNode->next = NULL;
*ppNext = pNode; // make previous node point to this one.
ppNext = &pNode->next; // update ppNext
}
else if (ch == '\n') {
// special handling of newline -- did you want to print the
// string and dump memory here?
}
} // end wile loop
// at this point we have a linked list with all characters that
// follow spaces -- print it out:
linkedListNode *pNode = pHead;
while (pNode != NULL) {
printf("%c", pNode->ch);
pNode = pNode->next;
}
printf("\n");
// free list:
pNode = pHead;
linkedListNode *pNext;
while (pNode) {
pNext = pNode->pNext;
free(pNode);
pNode = pNext;
}
免责声明:我没有测试过上面的内容,所以我可能犯了语法错误。无论如何,您看起来对 C 有点陌生,所以我强烈建议您将其绘制出来以确保您了解它是如何工作的。