函数和C-风格字符串

C风格字符串由一系列字符组成,以空值字符结尾。将字符串作为参数时意味着传递的是地址,但可以用const来禁止对字符串参数进行修改。

1.将C风格字符串作为参数的函数

假设要将字符串作为参数传递给函数,则表示字符串的方式有三种:

char数组;用引号括起的字符串常量(也称字符串字面值);被设置为字符串的地址的char指针。

但上述3种选择的类型都是char指针,准确的说是char*,因此可以将其作为字符串处理函数的参数:

char ghost[15] = 'galloping';
char* str = 'parkcar';
int n1 = strlen(ghost); //ghost = &ghost[0]
int n2 = strlen(str); //
int n3 = strlen('galloping') // 

可以说将字符串作为参数来传递,但实际传递的是字符串第一个字符的地址。这意味着字符串函数原型应将其表示字符串的形参声明为char*类型。

C风格字符串与常规char数组之间的一个重要区别是,字符串有内置的结束字符(包含字符,但不以空值字符结尾的char数组只是数组,而不是字符串)。这意味着不比将字符串长度作为参数传递给函数,而寒暑可以使用循环依次检查字符串中的每个字符,直到遇到结尾的空值字符为止。下面是使用一个函数来计算特定的字符在字符串中出现的次数,由于不需要处理负数,因此它将计数变量的类型声明为unsigned int。

#include <iostream>
unsigned int c_in_str(const char * str, char ch);
int main()
{
    using namespace std;
    char mmm[15] = "minimum";    // string in an array
// some systems require preceding char with static to
// enable array initialization

    char *wail = "ululate";    // wail points to string

    unsigned int ms = c_in_str(mmm, 'm');
    unsigned int us = c_in_str(wail, 'u');
    cout << ms << " m characters in " << mmm << endl;
    cout << us << " u characters in " << wail << endl;
    // cin.get();
    return 0;
}

// this function counts the number of ch characters
// in the string str
unsigned int c_in_str(const char * str, char ch)
{
    unsigned int count = 0;

    while (*str)        // quit when *str is '\0'
    {
        if (*str == ch)
            count++;
        str++;        // move pointer to next char
    }
    return count; 
}

c_in_ctr函数不应该修改原始字符串,因此它在声明形参str时使用了限定符const,这样,如果错误地址函数修改了字符串的内容,编译器将捕获这种错误,当然可以在函数中使用数组表示法,而不声明str:

unsigned int c_in_str(const char str[],char ch)

该函数演示了处理字符串中字符的标准形式:

while (*str)
{
    statements
    str++;
}

str最初执行字符串的第一个字符,因此*str表示的是第一个字符,例如,第一次调用该函数后,*str的值将为m--'minimum'第一个字符。只要字符不为空值字符(\0),*str就为非零值,因此循环将继续。在每轮循环的结尾处,表达式str++将指针增加一个字节,使之指向字符串中的下一个字符。最终,str将指向结尾的空值字符,使得*str等于0,空值字符的数字编码,从而结束循环。

2.返回C风格字符串的函数

现在,假设要编写一个返回字符串的函数,函数无法返回一个字符串,但可以返回字符串的地址,这样做的效率更高。

#include <iostream>
char * buildstr(char c, int n);     // prototype
int main()
{
    using namespace std;
    int times;
    char ch;

    cout << "Enter a character: ";
    cin >> ch;
    cout << "Enter an integer: ";
    cin >> times;
    char *ps = buildstr(ch, times);
    cout << ps << endl;
    delete [] ps;                   // free memory
    ps = buildstr('+', 20);         // reuse pointer
    cout << ps << "-DONE-" << ps << endl;
    delete [] ps;                   // free memory
    // cin.get();
    // cin.get();
    return 0;
}

// builds string made of n c characters
char * buildstr(char c, int n)
{
    char * pstr = new char[n + 1];
    pstr[n] = '\0';         // terminate string
    while (n-- > 0)
        pstr[n] = c;        // fill rest of string
    return pstr; 
}

buildstr函数返回一个指针,该函数接受两个参数,一个字符和一个数字,函数使用new创建一个长度与数字参数相等的字符串,然后将每个元素都初始化为该字符。然后,返回指向新字符串的指针。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值