[英]is there anything similar to Java's string 'charAt()' Method in C?
I'm trying to convert a piece of code from Java to C and I got stuck here, trying to get a character at each position. 我正在尝试将一段代码从Java转换为C,但我被困在这里,试图在每个位置获取一个字符。
char ch;
line += ' ';
while (pos < line.length())
{
ch = line.charAt(pos);
...
is there anything similar in C to convert the line ch = line.charAt(pos)
from java to C? 在C中有什么类似的东西可以将ch = line.charAt(pos)
从Java转换为C?
You can access the values as though the String was an array. 您可以像访问String一样访问值。
char str[] = "Hello World";
printf("%c", str[0]);
You can get a character at specific position in this way 您可以通过这种方式在特定位置获得角色
char str[] = "Anything";
printf("%c", str[0]);
but when you have a pointer array: 但是当您有一个指针数组时:
char* an_array_of_strings[]={"balloon", "whatever", "isnext"};
cout << an_array_of_strings[1][2] << endl;
If you need to change the strings use 如果您需要更改字符串,请使用
char an_array_of_strings[][20]={"balloon", "whatever", "isnext"};
cout << an_array_of_strings[1][2] << endl;
in C, the easiest way to get a char from an array of character (IE a string) 在C中,从字符数组(即字符串)中获取char的最简单方法
given the variables in your posted code, 给定您发布的代码中的变量,
char ch;
line += ' ';
while (pos < line.length())
{
ch = line.charAt(pos);
...
line[]
array for another character 假设line[]
数组中有另一个字符的空间 would become: 会成为:
#include <string.h>
strcat( line, " ");
size_t maxPos = strlen( line );
for( pos = 0; pos < maxPos; pos++ )
{
ch = line[pos];
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.