请问一下我怎样统计一个数组中分号出现的次数?char str[1024] = .............int i = str 中分号的个数?

解决方案 »

  1.   

    int getnum(char *str)
    {
       assert(str);
       char *lp = str;
       int result = 0;
       for(;;)
       {
           lp = strchr(lp, ';');
           if(!lp) return result;
           lp = ++lp;
       }
       return result;
    }
      

  2.   

    int getnum(char *str)
    {
       assert(str);
       char *lp = str;
       int result = 0;
       for(;;)
       {
           lp = strchr(lp, ';');
           if(!lp) return result;
           lp = ++lp;
           result++;
       }
       return result;
    }
      

  3.   

    CString str1=str;
    int i=str1.Replace(';',';');
      

  4.   

    不说清楚,给你源代码你直接看
    int CString::Replace(TCHAR chOld, TCHAR chNew)
    {
    int nCount = 0; // short-circuit the nop case
    if (chOld != chNew)
    {
    // otherwise modify each character that matches in the string
    CopyBeforeWrite();
    LPTSTR psz = m_pchData;
    LPTSTR pszEnd = psz + GetData()->nDataLength;
    while (psz < pszEnd)
    {
    // replace instances of the specified character only
    if (*psz == chOld)
    {
    *psz = chNew;
    nCount++;
    }
    psz = _tcsinc(psz);
    }
    }
    return nCount;
    }