c

来源:互联网 发布:sql 删除列的默认值 编辑:程序博客网 时间:2024/04/27 02:41

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

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include <iostream.h>
#include <malloc.h>

typedef struct List
{
 void   *data;
 struct List *next;

}List;

FILE *stream;
char** f1();
int f2();
int index(char* value,const char *substring);
int f3();
char* substring(const int start, const unsigned int end);
int split(char string[],const char *chars,char* substr[]);
int getList(List *list,int len,char* initData);
char* getArray();


int main( void )
{
// f2();
// f3();
/*
   char*   s= "我this啊 is a string";

   stream = fopen( "c://c1.text", "w" );
   fprintf( stream, "%s", s);
   fclose( stream );
  printf("%s/n",s);
*/
/**************************************
 //unsigned short str[50];
 char str[100];
 char s[]="xmlns";
 stream=fopen( "c://c1.text", "r" );
 while(!feof(stream))
 { 
  fgets(str,100,stream);  //逐行读取
  printf("%s", str);
  printf(".%d",strlen(str));
  char* p1=strstr(str,s);
  if(p1!=NULL)
   printf("%s",p1);
 }
 fclose(stream);
**************************************
 char string[]="akjf,akdjfadfj,djfk";
 char* seps=",a";
 char* s[10];
 int n=split(string,seps,s);
 printf("s=%s/n",s[2]);
 printf("n=%d",n);
   return 1;
   ********************************
 char* text;
 int x=5;
 text=(char*)malloc((x+1)*sizeof(char));
 for(int i=0;i<5;i++)
  text[i]='a';
 text[i]=0;
 printf("%s",text);
 printf("=%d/n",strlen(text));
 printf("%d",sizeof(int));

****************************
 char *str=getArray();
 printf("%s.",str);
******************************/

 List *l ,*p;
 getList(l,15,"123456");
 p=l;
 while(p!=NULL)
 {
  printf("%s/n",(char *)p->data);
 }

 getchar();
 return 0;
}
char* getArray()
{
 //char array[20]="who are you?";
 char *array=(char *)malloc(20);
 strcpy(array,"hehe");
// char *array="hehehehe";
 return array;
}

int getList(List *list,int len,char* initData)
{
 List first,*pList;
 char *dd=(char *)malloc(80);
 if(dd==NULL)
 {
  printf("malloc error!");
  return 0;
 }
 strcpy(dd,initData);
 first.data=(void *)dd;
 first.next=NULL;
 list=&first;

 pList=&first;
 for(int i=1;i<len;i++)
 { List item;
  char *dd1=(char *)malloc(10);
  strcpy(dd1,initData);
  sprintf(dd1,"%d: %s",i,dd);
  item.next=NULL;
  pList->next=&item;
  pList=&item;
 }

 return len;
}

//用字符集中的字符分隔字符串
int split(char string[],const char *seps,char* substr[])
{
 int i=0;
 substr[i]=strtok(string,seps);
// printf("%s/n",substr[i]);
 while(substr[i]!=NULL)
 {
  cout<<substr[i]<<endl;
  i++;
  substr[i]=strtok(NULL,seps);
 }
 return i;
}

//模式匹配:strstr
f2()
{
char str[] =    "tazy";
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] =   "         1         2         3         4         5";
char fmt2[] =   "12345678901234567890123456789012345678901234567890";

   char *pdest;
   int  result;
 result=index(string,str);
 printf("result=%d",result);
 pdest=strstr(string,str);
 printf("%s",pdest);
 return 0;
}
//自定义的子串定位函数
int index(char* value,const char *substring)
{
 char* pIndex=strstr(value,substring);
 int i=pIndex-value+1;
 if(pIndex!=NULL)
  return i-1;
 else
  return -1;
}

f3()
{
 char* s=substring(2,19);
 printf("%s/n",s);
 return 0;
}
//自定义的求子串函数
char* substring(const int start, const unsigned int end)
{
 char* value="The quick brown dog jumps over the lazy fox";
 int length=end-start;
 if(end>strlen(value))
  length=strlen(value)-start;
 char* str=(char*)malloc(length+1);
 strncpy(str,value+start,length);
 strnset(str+length,'/0',1);
 //printf("str.length=%d",end-start+1);
 return str;
}
/*****************************************/

 

原创粉丝点击