c ++查找字符串
Program 1:
程序1:
#include <iostream>
using namespace std;
int main()
{
char A[5] = { 'H', 'E', 'L', 'L', 'O' };
char ch;
ch = (A + 2)[2];
cout << ch;
return 0;
}
Output:
输出:
O
Explanation:
说明:
Here, we created a character array initialized with some character values. Now look the below statement,
在这里,我们创建了一个用一些字符值初始化的字符数组。 现在看下面的语句,
ch = (A+2)[2];
In the above statement, we are accessing the element of index 4. That is 'O'. Then finally 'O' will be printed on the console screen.
在上面的语句中,我们正在访问索引4的元素。即'O' 。 最后, “ O”将被打印在控制台屏幕上。
Program 2:
程式2:
#include <iostream>
using namespace std;
int main()
{
char A[2][3] = { 'H', 'E', 'L', 'L', 'O' };
cout << A[1][0] << " " << A[1][2] << " ";
return 0;
}
Output:
输出:
L
Explanation:
说明:
Here, we created a two-dimensional character array initialized with some character values.
在这里,我们创建了一个二维字符数组,并使用一些字符值进行了初始化。
Here the values of array will be like this,
这里array的值将像这样,
A[0][0] = 'H';
A[0][1] = 'E';
A[0][2] = 'L';
A[1][0] = 'L';
A[1][1] = 'O';
A[1][2] = '\0';
So,
A[1][0] = 'L' and A[1][2] = '\0';
Then, the L will print, but '\0' character cannot print on the console screen.
然后,将打印L ,但无法在控制台屏幕上打印'\ 0'字符。
Program 3:
程式3:
#include <iostream>
using namespace std;
int main()
{
char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };
cout << STR[1];
return 0;
}
Output:
输出:
main.cpp:6:21: warning: character constant too long for its type
char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };
^~~~~~~
main.cpp:6:30: warning: multi-character character constant [-Wmultichar]
char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };
^~~~~~
main.cpp:6:38: warning: multi-character character constant [-Wmultichar]
char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };
^~~~~
main.cpp:6:45: warning: character constant too long for its type
char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };
^~~~~~~
main.cpp:6:54: warning: character constant too long for its type
char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };
^~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:6:62: error: invalid conversion from ‘int’ to ‘char*’ [-fpermissive]
char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };
^
main.cpp:6:62: error: invalid conversion from ‘int’ to ‘char*’ [-fpermissive]
main.cpp:6:62: error: invalid conversion from ‘int’ to ‘char*’ [-fpermissive]
main.cpp:6:62: error: invalid conversion from ‘int’ to ‘char*’ [-fpermissive]
main.cpp:6:62: error: invalid conversion from ‘int’ to ‘char*’ [-fpermissive]
Explanation:
说明:
The above code will generate an error (invalid conversion from 'int' to 'char*' [-fpermissive]) because we enclosed string values within a single quote that is not the correct way. We can enclose a string within double-quotes.
上面的代码将产生错误( 从'int'到'char *'[-fpermissive]的无效转换 ),因为我们将字符串值括在单引号中,这是不正确的方式。 我们可以将字符串括在双引号中。
Recommended posts
推荐的帖子
翻译自: https://www.includehelp.com/cpp-tutorial/arrays-find-output-programs-set-4.aspx
c ++查找字符串