鱼C论坛

 找回密码
 立即注册

C分解字符串---exam01

已有 534 次阅读2015-3-27 00:11 |个人分类:C

1 编写一个业务函数,实现字符串(前后各有三个空格,单词前后也均有空格)
"   i am student, you are teacher   " ,
各个单词首字符大写,结果如下"   I Am Student, You Are Teacher   " ,
//写了一个多小时,才写出来这么点,很多地方不规范,功能还没实现,逗号后的大写还没实现,指针也没释放,待续。。。。
//2015-04-06,基本功能已经实现,如果指针数组定义在从函数,则指针释放有问题
//如果在从函数里定义指针数组并作为返回值,则在vs2010报warning C4172: returning address of local variable or temporary
//warning C4172: 返回局部变量或临时变量的地址
//在初始话该变量时malloc一下再使用这个变量。
//例如char *szString = (char *)malloc(100),这样在函数结束时该变量就不会被释放。

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char** getArray(const char* pIn, const char* pKey,int* length);//匈牙利命名法
void trimspace(char* ptrimstring);//输入字符串“   i love fishc   ”,去掉前后的3个空格,
int main()
{
    char csource[100];   
    char *pSource = csource;
    char **pArray = NULL;
    int nLength = 0;
    int i =0,j = 0;
    char a = ' ';
    printf("please input the strings start and end with 3 spaces respectively:\n");
    gets(pSource);
    //trim the space before and after the string geted
    trimspace(pSource);   
    nLength = strlen(csource);
    //split the string with ' ' and puts the eltemnets of the pointer array pointed to the string splited
    pArray = getArray(pSource,&a,&nLength);
    //change to xiaoxe
    for (i = 0;i < nLength; i++)
    {
        pArray[i][0] = pArray[i][0] -32;
        printf("%s ",pArray[i]);
        j=j+strlen(pArray[i]);       
    }
    printf("\nthe numbers of the string is %d:",j);
    system("pause");
    return 0;
}

char** getArray( const char* pIn, const char* pKey,int *length)
{
    int i = 0;
    char* pExtract[100];//这是个临时变量不安全,作为返回值的话不安全
    //可以这样定义 char** pExtract = (char**)malloc(10*sizeof(char*))就可以解决释放的问题
    const char *p1 = NULL;
    const char *p2 = NULL;
    //p1 and p2 point to the header of the string processed.
    p1 = p2 = pIn;
    //p2!=p1 is used for the last string
    while (*p2!='\0' || p2 != p1)
    {
        if ((*p2) != ' ' && (*p2) != '\0')
        {
            p2++;
        }
        //else malloc memory and store the sub-string
        else
        {
            pExtract[i] = (char*)malloc((p2-p1+1)*sizeof(char));
            strncpy(pExtract[i],p1,p2-p1);
            *(pExtract[i]+(p2-p1)) = '\0';
            i = i+1;
            if ((*p2) == '\0')
                break;
            else
            {    p2++;
                p1 = p2;
            }
        }
    }
    *length = i;
    return pExtract;
}
void trimspace(char* ptrimstring)
{
    int iLen;
    int i = 0;
    iLen = strlen(ptrimstring);
    for (;i < iLen-6;i++)
    {
        ptrimstring[i] = ptrimstring[i+3];
    }
    ptrimstring[i] = '\0';
}

-------------方法2,把指针数组定义在了主函数里,解决定义在从函数中释放老报错的问题,原因返回局部变量临时变量-------------

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void getArray(const char* pIn, const char* pKey,int* length,char** pA2);//匈牙利命名法
void trimspace(char* ptrimstring);//输入字符串“   i love fishc   ”,去掉前后的3个空格
void includingcomma(char* pincluding);//sub-string including comma(逗号)
void freepointer(char* pfreep);
void free2cpointer(char** pfree2p,int inumbers);
int main()

{
    char csource[100];
    char *pSource = csource;
    char **pArray = NULL;   
    char* pExtract[100];   
    int nLength = 0;
    int i =0,j = 0;
    char a = ' ';
    pArray = pExtract;
    printf("please input the strings start and end with 3 spaces respectively:\n");
    gets(pSource);
    //trim the space before and after the string geted
    trimspace(pSource);   
    nLength = strlen(csource);
    //split the string with ' ' and puts the eltemnets of the pointer array pointed to the string splited
    getArray(pSource,&a,&nLength,pArray);
    //pTemp = pArray;
    //change to xiaoxe
    for (i = 0;i < nLength; i++)
    {
        pArray[i][0] = pArray[i][0] -32;
        includingcomma(pArray[i]);
        printf("%s ",pArray[i]);       
        j=j+strlen(pArray[i]);           
    }
    //below call and free is error
    //     for (i = nLength-1;i >= 0; i--),
    //     {
    //         pTemp = pArray +i;
    //         freepointer(*pTemp)    ;
    //     }
    //     if (pArray[0]!=NULL)
    //         free(pArray[0]);
    //     if (pArray[1]!=NULL)
    //         free(pArray[1]);
    //     if (pArray[2]!=NULL)
    //         free(pArray[2]);
    free2cpointer(pArray,nLength);
    printf("\nthe length of the string is %d:",j);
    system("pause");
    return 0;
}

void getArray( const char* pIn, const char* pKey,int *length,char** pA2)
{
    int i = 0;
    char **pExtract = pA2;
    const char *p1 = NULL;
    const char *p2 = NULL;
   
    //p1 and p2 point to the header of the string processed.
    p1 = p2 = pIn;
    //p2!=p1 is used for the last string
    while (*p2!='\0' || p2 != p1)
    {
        if ((*p2) != ' ' && (*p2) != '\0')
        {
            p2++;
        }
        //else malloc memory and store the sub-string
        else
        {
            pExtract[i] = (char*)malloc((p2-p1+1)*sizeof(char));
            strncpy(pExtract[i],p1,p2-p1);
            *(pExtract[i]+(p2-p1)) = '\0';
            i = i+1;
            if ((*p2) == '\0')
                break;
            else
            {    p2++;
            p1 = p2;
            }
        }
    }
    *length = i;
    //return pExtract;
}
void trimspace(char* ptrimstring)
{
    int iLen;
    int i = 0;
    iLen = strlen(ptrimstring);
    for (;i < iLen-6;i++)
    {
        ptrimstring[i] = ptrimstring[i+3];
    }
    ptrimstring[i] = '\0';
}
void includingcomma(char* pincluding)
{
    int iindex = 0, ilength = 0;
    if (pincluding == NULL)
    {
        return ;
    }
    ilength=strlen(pincluding);
    for (;iindex < ilength;iindex++)
    {
        if (pincluding[iindex] == ',')
        {
            pincluding[iindex+1] = pincluding[iindex+1] - 32;
        }
    }
}
void freepointer(char* pfreep)
{
    if (pfreep != NULL)
        free(pfreep);   
}
void free2cpointer(char** pfree2p,int inumbers)
{
    int icount = 0;
    for(icount=0;icount<inumbers;icount++)
    {
        if(pfree2p[icount] != NULL)
            {
                free(pfree2p[icount]);
                pfree2p[icount] = NULL;
            }
    }

}

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-6 09:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部