下列字符串使用指针 设计或构成
下列字符串使用 指针 设计或构成:
1. void strcpy()
2. int strcmo()
3. char *strrev()
4. char*strchr()
5. char *strstr()
[解决办法]
void strcpy(char* des, const char* src){
if((src==NULL) || des==NULL) return;
while(*src!= '\0 '){
*des++ = *src++;
}
}
[解决办法]
int strcmp(const char* s1, const char* s2){
unsigned len1 = strlen(s1);
unsigned len2 = strlen(s2);
if((len1==0) && (len2==0)) return 0; //return 0 means equal
if((len1==0) &&(len2> 0)) return -1; //-1 means s1 <s2
if((len1> 0) && (len2==0)) return 1; //1 means s1> s2
unsigned len = len1> len2?len2:len1;
cout < < "lenth: " < <len < <endl;
for(int i=0;i <len;i++){
if(*s1> *s2) return 1;
if(*s1 <*s2) return -1;
s1++;
s2++;
}
if(len1==len2) return 0;
if(len1> len2) return 1; //the longer the bigger
return -1;
}
[解决办法]
char *strrev(char *str)
{
char s[2],*t;
if(strlen(str)==1)
return &str[0];
s[0]=str[0];
s[1]= '\0 ';
t=strrev((str+1));
strcat(t,s);
return t;
}
[解决办法]
参考 一些库的实现。
/***
*strcmp.c - routine to compare two strings (for equal, less, or greater)
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* Compares two string, determining their lexical order.
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
#ifdef _MSC_VER
#pragma function(strcmp)
#endif /* _MSC_VER */
/***
*strcmp - compare two strings, returning less than, equal to, or greater than
*
*Purpose:
* STRCMP compares two strings and returns an integer
* to indicate whether the first is less than the second, the two are
* equal, or whether the first is greater than the second.
*
* Comparison is done byte by byte on an UNSIGNED basis, which is to
* say that Null (0) is less than any other character (1-255).
*
*Entry:
* const char * src - string for left-hand side of comparison
* const char * dst - string for right-hand side of comparison
*
*Exit:
* returns -1 if src < dst
* returns 0 if src == dst
* returns +1 if src > dst
*
*Exceptions:
*
*******************************************************************************/
int __cdecl strcmp (
const char * src,
const char * dst
)
{
int ret = 0 ;
while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src, ++dst;
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
[解决办法]
/***
*strchr.c - search a string for a given character
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines strchr() - search a string for a character
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
/***
*char *strchr(string, c) - search a string for a character
*
*Purpose:
* Searches a string for a given character, which may be the
* null character '\0 '.
*
*Entry:
* char *string - string to search in
* char c - character to search for
*
*Exit:
* returns pointer to the first occurence of c in string
* returns NULL if c does not occur in string
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl strchr (
const char * string,
int ch
)
{
while (*string && *string != (char)ch)
string++;
if (*string == (char)ch)
return((char *)string);
return(NULL);
}
[解决办法]
/***
*strstr.c - search for one string inside another
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines strstr() - search for one string inside another
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
/***
*char *strstr(string1, string2) - search for string2 in string1
*
*Purpose:
* finds the first occurrence of string2 in string1
*
*Entry:
* char *string1 - string to search in
* char *string2 - string to search for
*
*Exit:
* returns a pointer to the first occurrence of string2 in
* string1, or NULL if string2 does not occur in string1
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl strstr (
const char * str1,
const char * str2
)
{
char *cp = (char *) str1;
char *s1, *s2;
if ( !*str2 )
return((char *)str1);
while (*cp)
{
s1 = cp;
s2 = (char *) str2;
while ( *s1 && *s2 && !(*s1-*s2) )
s1++, s2++;
if (!*s2)
return(cp);
cp++;
}
return(NULL);
}
[解决办法]
/***
*strrev.c - reverse a string in place
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines _strrev() - reverse a string in place (not including
* '\0 ' character)
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
/***
*char *_strrev(string) - reverse a string in place
*
*Purpose:
* Reverses the order of characters in the string. The terminating
* null character remains in place.
*
*Entry:
* char *string - string to reverse
*
*Exit:
* returns string - now with reversed characters
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl _strrev (
char * string
)
{
char *start = string;
char *left = string;
char ch;
while (*string++) /* find end of string */
;
string -= 2;
while (left < string)
{
ch = *left;
*left++ = *string;
*string-- = ch;
}
return(start);
}
[解决办法]
char * __cdecl strcpy (
char * dest,
const char * source
)
{
char *start = dest;
while (*dest++ = *source++) /* copy string */
*dest++ = '\0 ';
return(start);
}