#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <sys/stat.h>
void getdate(char *datestr,char *format)
{
time_t nnowtime = time(NULL);
struct tm* ptmTemp = localtime(&nnowtime);
if (ptmTemp == NULL ||
!strftime(datestr, 256, format, ptmTemp))
datestr[0] = '\0';
}
void StrToLower(char * Str)
{
int len;
int i;
if(Str == NULL) return;
len = (int)strlen(Str);
if(len <= 0) return;
for(i=0; i<len; i++)
{
if((Str[i] >= 'A') && (Str[i] <= 'Z'))
Str[i] = 'a' + Str[i] - 'A';
}
return;
}
bool StrIsDigit(char * Str)
{
char digitS[11]="0123456789";
int i = 0;
for(i = 0; i < (int)strlen(Str); i++)
{
if(strchr(digitS,Str[i]) == NULL)
return false;
}
return true;
}
/**********************************************************************************
* Function:
* 1. Replace the first 'oldstr' with 'newstr' in 'srcstr'
* Arguments:
* IN :
* srcstr
* oldstr
* newstr
* OUT :
* No
* Return:
* 1. If find and replace 'oldstr' with 'newstr' in 'srcstr', return 1
* 2. If find no 'oldstr' in 'srcstr', return 0
* 3. If error (malloc return NULL) return -1
***********************************************************************************/
int StrReplace(char * srcstr, const char * oldstr, const char * newstr)
{
char *tmpbuffer;
int prelen, postlen, totallen, newlen, oldlen;
char *ptr;
char *tmpchar;
tmpchar = strstr(srcstr,oldstr);
if(tmpchar == NULL) return 0;
totallen=(int)strlen(srcstr);
oldlen=(int)strlen(oldstr);
newlen=(int)strlen(newstr);
prelen = (int) (tmpchar - srcstr);
postlen = totallen - prelen - oldlen;
tmpchar += oldlen;
tmpbuffer = (char*)malloc(prelen + newlen + postlen+1);
if(tmpbuffer == NULL) return -1;
ptr = tmpbuffer;
memcpy(ptr, srcstr, prelen);
ptr += prelen;
memcpy(ptr, newstr, newlen);
ptr += newlen;
memcpy(ptr, tmpchar, postlen);
tmpbuffer[prelen + newlen + postlen] = 0;
strcpy(srcstr, tmpbuffer);
srcstr[prelen + newlen + postlen]=0;
free((void*)tmpbuffer);
return 1;
}
#include <iostream>
using namespace std;
int main()
{
char szdate[64]={0};
char sztime[64]={0};
getdate(szdate, "%Y%m%d");
getdate(sztime, "%H%M%S");
std::cout<<szdate<<std::endl;
std::cout<<sztime<<std::endl;
char a = 'A';
if(StrIsDigit(&a))
std::cout<<"ewfa"<<std::endl;
StrToLower(&a);
std::cout<<a<<std::endl;
char str123[100];
strcpy(str123," my name my your name is hellowrold");
StrReplace(str123,"my","your");
std::cout<<str123<<std::endl;
return 0;
}