有没有类似于Java的字符串’charAt()’方法在C?

我正在尝试将一段代码从Java转换为C并且我被困在这里,试图在每个位置获得一个角色。

char ch; line += ' '; while (pos < line.length()) { ch = line.charAt(pos); ... 

在C中是否有类似的东西将ch = line.charAt(pos)从java转换为C?

在C中,从字符数组中获取char的最简单方法(IE是一个字符串)

给出发布代码中的变量,

 char ch; line += ' '; while (pos < line.length()) { ch = line.charAt(pos); ... 
  1. 假设字符串以NUL字节('\ 0')终止
  2. 假设line[]数组中有另一个字符的空间

会成为:

 #include  strcat( line, " "); size_t maxPos = strlen( line ); for( pos = 0; pos < maxPos; pos++ ) { ch = line[pos]; .... 

您可以像访问String一样访问值。

 char str[] = "Hello World"; printf("%c", str[0]); 

您可以通过这种方式获得特定位置的角色

 char str[] = "Anything"; printf("%c", str[0]); 

但是当你有一个指针数组时:

 char* an_array_of_strings[]={"balloon", "whatever", "isnext"}; cout << an_array_of_strings[1][2] << endl; 

如果需要更改字符串使用

 char an_array_of_strings[][20]={"balloon", "whatever", "isnext"}; cout << an_array_of_strings[1][2] << endl; 

来源: 这里