# 自已设计接口并实现之（不能用系统函数）,该如何处理

www.MyException.Cn  网友分享于：2013-02-28  浏览：5次

1.将一个字符串的所有字符转换成小写字母？
2.定义一个二叉树，并求解其叶子节点数目？

------解决方案--------------------

void strtolower(const char * src, char * dst)
{
int i = 0;

while (src[i] != '\0')
{
dst[i] = src[i] >= 'A' && src[i] <= 'Z' ? src[i] - 'A' + 'a' : src[i];
++i;
}

dst[i] = '\0';
}
------解决方案--------------------
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

/*define the flag for program */
#define ERROR NULL

int sum = 0;

typedef int DATA;

struct node{
DATA d;
struct node *left;
struct node *right;
};

typedef struct node NODE;
typedef NODE *BTREE;

/* Creating a binary tree. */
BTREE new_node(void)
{
return (NODE *)malloc(sizeof(NODE));
}

BTREE init_node(DATA d1,BTREE p1,BTREE p2)
{
BTREE t;

t = new_node();

if(t == NULL)
{
return ERROR;
}

t->d = d1;
t->left = p1;
t->right = p2;

return t;
}

/* Creating a linked binary tree from an array. */
BTREE create_tree(DATA a[],int i,int size)
{
if(i >= size)
return NULL;
else
return init_node(a[i],create_tree(a,2*i+1,size),create_tree(a,2*i+2,size));
}

void sumAllNodes(BTREE root)
{
if(root != NULL)
{
sumAllNodes(root->left);

if(root->left == NULL && root->right == NULL)
{
sum++;
}
sumAllNodes(root->right);

}

}

void main(void)
{
int binaryArr[10] = {5,6,7,8,9,10,11,12,15,18};
BTREE BT = create_tree(binaryArr,0,10);
sumAllNodes(BT);
printf("\n%d\n",sum);
getch();
}
------解决方案--------------------

string 类的方法去转
------解决方案--------------------

private override string CharToLower(string str)
{
StringBuilder strBui = new StringBuilder(str);
for (int i = 0; i < strBui.Length; i++)
{
if (strBui[i] >= 65 && strBui[i] <= 90)
{
strBui[i] = (char)(strBui[i] + 32);
}
}

return strBui.ToString();
}