c风格字符串表达形式
const char* str = "mamamiya";
char ghost[] = "youpomian";
c风格字符串作为参数
#define _CRT_SECURE_NO_WARNINGS#include <iostream>
#include<string>unsigned int c_in_str(const char* str, char ch);
int main()
{using namespace std;const char* str = "mamamiya";unsigned int ms = c_in_str(str,'m');unsigned int as = c_in_str(str,'a');cout << ms << " m characters in " << str << endl;cout << as << " a characters in " << str << endl;return 0;
}
unsigned int c_in_str(const char* str, char ch)
{unsigned int count = 0;while (*str){if (*str == ch)count++;str++;}return count;
}
返回c风格字符串
#define _CRT_SECURE_NO_WARNINGS#include <iostream>
#include<string>
char* ret_str(char ch, int size);
int main()
{using namespace std;char * str = ret_str('c', 5);return 0;
}
char* ret_str(char ch,int size)
{char* pstr = new char[size+1];pstr[size] = '\0';while (size-- > 0){pstr[size] = ch;}return pstr;
}