1.strlen
int strlen(char *str)
{
char *p = str;
while(*p++);
return p-str-1;
}
2.strcat
void strcat(char *str1,char *str2)
{
while(*str1++);
str1--;
while(*str1++=*str2++);
}
3.strcpy
char *strcpy(char *dest,char *src)
{
char *p = dest;
while(*dest++=*src++);
return p;
}
4.strcmp
int strcmp(const char *str1,const char *str2)
{
while(!(ret=*(unsigned char*)str1-*(unsigned char*str2) && str2)){//不相等,退出循环
str1++;
str2++;
}
if(ret<0)
ret = -1;
if(ret>0)
ret = 1;
return ret;
}
6.memcpy
#include <stdio.h>
#include <stdlib.h>
void *myMemcpy(void *dest,const void *src,int n)
{
void *ret = dest;
if(dest==NULL || src==NULL) return NULL;
while(n--) *(char *)dest++ = *(char *)src++;
return ret;
}
int main()
{
char arr[] = {"helloword"};
int size = sizeof(arr)/sizeof(arr[0]);
char *pdest = (char *)malloc(128*sizeof(char));
char *ret = myMemcpy(pdest,arr,size);
printf("%s\n",ret);
return 0;
}
5.翻转字符串
void reverseString(char *a,int size)
{
int start = 0;
int end = size-1;
char tmp;
while(start < end){
tmp = a[start];
a[start] = a[end];
a[end] = tmp;
start++;
end--;
}
return 0;
}
7.九九乘法表
#include <stdio.h>
int main()
{
int i = 0;
int j = 0;
for (i = 1; i <=9; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d*%d=%-4d",j,i,j*i);
}
printf("\n");
}
return 0;
}
8.二分查找
int binSearch(int *a,int x,int low,int high)
{
int mid;
if(low>high)
return -1;
mid = (low+high)/2;
if(x==a[mid])
return mid;
if(x<a[mid])
return binSearch(a,x,low,mid-1);
else
return binSearch(a,x,mid+1,high);
}
9.回文串判断
#include <stdio.h>
#include <string.h>
int main()
{
char str[50]="abcba";
int flag=0;
int len = strlen(str);
for(int i=0;i<len /2;i++){
if(str[i]=str[len-i-1])
flag = 1;
else{
flag = 0;
break;
}
}
if(flag) printf("yes\n");
else printf("no\n");
}
10.选择排序
void selectSort(int *a,int n)
{
for(int i=0;i<n-1;i++)
{
int min = i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
tmp = a[min];
a[min] = a[j];
a[j] = tmp;
}
}
}
}
11.冒泡排序
void bubbleSort(int *a,int n)
{
char i,j,flag;
char tmp = 0;
for(i=0;i<n-1;i++)
{
flag = 0;
for(j=0;j<n-i-1;j++)
{
if(a[j+1]<a[j])
{
tmp = a[j+1];
a[j+1]=a[j];
a[j]=tmp;
}
flag = 1;
}
if(!flag) break;
}
}
12.快速排序
#include <stdio.h>
#define N 6
int support(int *a,int left,int right)
{
int tmp;
int key = a[left];
int start = left;
while(left != right){
while(a[right]>=key && right>left) right--;
while(a[left]<=key && left<right) left++;
if(right>left){
tmp = a[left];
a[left] = a[right];
a[right] = tmp;
}
}
a[start] = a[left];
a[left] = key;
return left;
}
void quickSort(int *a,int left,int right)
{
int mid;
if(left >= right) return;
mid = support(a,left,right);
quickSort(a,left,mid-1);
quickSort(a,mid+1,right);
}
int main()
{
int i,j;
int a[N]={0};
for(i=0;i<N;i++)
scanf("%d",&a[i]);
quickSort(a,0,N-1);
for(i=0;i<N;i++)
printf("%d ",a[i]);
return 0;
}
13.链表
#include <stdio.h>
typdef struct node
{
int data;
struct node *next;
}Node;
//头插法
Node *insertForward(Node *head,Node *new)
{
if(head == NULL){
head = new;
}else{
new->next = head;
head = new;
}
return head;
}
//尾插法
Node *insertBehind(Node *head,Node *new)
{
Node *p = head;
if(head == NULL){
head = new;
}else{
while(p->next != NULL){
p = p->next;
}
p->next = new;
}
return head;
}
//创建链表
void createList(Node *head)
{
int i;
for(i=0;i<5;i++){
Node *new = (Node *)malloc(sizeof(Node));
scanf("%d",&new->data);
new->next = NULL;
insertBehind(head,new);
}
}
//删除节点
Node *deleteNode(Node *head,int data)
{
Node *p = head;
if(head->data = data){
head = head->next;
}else{
while(p->next != NULL){
if(p->next->data == data){
p->next = p->next->next;
}
p = p->next;
}
}
return head;
}
//翻转链表
Node *reverseList(Node *head)
{
Node *left = NULL;
Node *mid = head;
Node *right = NULL;
if(head == NULL){
return head;
}else{
while(mid->next != NULL){
right->next = mid->next;
mid->next = left;
left = mid;
mid = right;
}
return left;
}
}
//遍历链表
void printList(Node *head)
{
Node *p = head;
while(p->next != NULL){
printf("%d\t",p->data);
p = p->next;
}
}