69,372
社区成员
发帖
与我相关
我的任务
分享
/*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);
}
// 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;
}