C语言录入无限制长度字符串?

NONAME7943 2008-11-03 11:19:50
題目是錄入無限制個字符放在一個字符串內, 并統計輸入了多少個小寫字母.
可是我寫的每次都是到第100個字符的時候就不能再錄入了, 請教一下這是哪裡有問題..

以下為我的代碼:
/*Written by Kai C. Sept. 16, 2008*/
/*Edited on Nov. 2, 2008*/
/*Email:ck.nonamestudio@gmail.com*/

#include <stdio.h>

void main(void)
{
char* str;
char* tmpStr;
int i = 0, sum = 0, n = 0;

str = malloc(5);

printf("Enter a string (don't mind the lenth):\n");

do
{
/*re-allocate memory*/
if(n % 5 == 0 && n != 0)
{
tmpStr = malloc(n);
for(i = 0; i <= n; i++)
{
tmpStr[i] = str[i];
}

/*str = realloc(n + 5, sizeof(char));*/
free(str);
str = malloc(n + 5);

for(i = 0; i <= n; i++)
{
str[i] = tmpStr[i];
}

free(tmpStr);
}

scanf("%c", &str[n]);
}while(str[n++] != '\n');

/*c-string terminated by Null character*/
str[n - 1] = '\0';

printf("The string you just entered is:%s\n", str);

i = 0;
while(str[i])
{
if(str[i] >= 'a' && str[i] <= 'z')
{
sum++;
}
i++;
}

printf("You entered %d small letters.\n", sum);

free(str);
}


...全文
427 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
DaPPer 2012-02-27
  • 打赏
  • 举报
回复

// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

typedef struct LNode
{
int data;
struct LNode *next;
}LNode;
int _tmain(int argc, _TCHAR* argv[])
{
LNode *strp;
strp = (LNode *)malloc(sizeof(LNode));
char YN = '\0';
//cout << strp->data << endl;
while (1)
{
cin >> strp->data;
cout << strp->data << endl;
strp = strp->next;
strp = (LNode *)malloc(sizeof(LNode));
cout << "是否停止输入(Y/N):" << endl;
cin >> YN;
if ('Y' == YN || 'y' == YN)
{
break;
}
else if ('n' == YN || 'N' == YN)
{
continue;
}
}

return 0;
}

yao2007 2008-11-03
  • 打赏
  • 举报
回复
我在VC6.0里也没问题。正确的吧。
NONAME7943 2008-11-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jiean 的回复:]
在我机器上没有问题,VC6.0
[/Quote]

我懷疑是TC的問題
我在VC++2008里運行, 加上malloc.h, 把malloc()改成(char *)malloc()就能輸入無限字符了.

可是, 要是在C++里, 早就可以直接用String類了...
Larry316 2008-11-03
  • 打赏
  • 举报
回复
#include <stdio.h>
#include<malloc.h>////////////////
void main(void)
{
char* str;
char* tmpStr;
int i = 0, sum = 0, n = 0;

str =(char *)malloc(5);//////////////////////////////////

printf("Enter a string (don't mind the lenth):\n");

do
{
/*re-allocate memory*/
if(n % 5 == 0 && n != 0)
{
tmpStr =(char *) malloc(n);/////////////////////////////////
for(i = 0; i <= n; i++)
{
tmpStr[i] = str[i];
}

/*str = realloc(n + 5, sizeof(char));*/
free(str);
str = (char *)malloc(n + 5);////////////////////////////////////

for(i = 0; i <= n; i++)
{
str[i] = tmpStr[i];
}

free(tmpStr);
}

scanf("%c", &str[n]);
}while(str[n++] != '\n');

/*c-string terminated by Null character*/
str[n - 1] = '\0';

printf("The string you just entered is:%s\n", str);

i = 0;
while(str[i])
{
if(str[i] >= 'a' && str[i] <= 'z')
{
sum++;
}
i++;
}

printf("You entered %d small letters.\n", sum);

free(str);
}
现在就好使了!!
jiean 2008-11-03
  • 打赏
  • 举报
回复
在我机器上没有问题,VC6.0
NONAME7943 2008-11-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 clhposs 的回复:]
录入无限 如果是我 我就用链表

struct test
{
      char ch;
      struct test *next;
}
[/Quote]

麻煩能不能詳細解釋一下啊, 我剛學C和C++(兩個一塊學...C++ Primer剛看到第5章...) 原來一直用VB...
macfan 2008-11-03
  • 打赏
  • 举报
回复
录入无限长是不可能的吧...这要受到编译器和内存等等的限制..
clhposs 2008-11-03
  • 打赏
  • 举报
回复
录入无限 如果是我 我就用链表

struct test
{
char ch;
struct test *next;
}
NONAME7943 2008-11-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 shinefree2004 的回复:]
#include <malloc.h>是很好的习惯
[/Quote]

我這TC2.0貌似沒malloc.h......
shinefree2004 2008-11-03
  • 打赏
  • 举报
回复
#include <malloc.h>是很好的习惯

69,372

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧