是否有类似于Java的字符串'charAt()'方法在C? [英] is there anything similar to Java's string 'charAt()' Method in C?

查看:118
本文介绍了是否有类似于Java的字符串'charAt()'方法在C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一段代码从Java转换为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.

char ch;

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

在C中有任何类似的转换行 ch = line.charAt(pos)从java到C?

is there anything similar in C to convert the line ch = line.charAt(pos) from java to C?

推荐答案

在C中,给出字符数组中的char的最简单方法(IE为字符串)

in C, the easiest way to get a char from an array of character (I.E. a string)

给出已发布代码中的变量,

given the variables in your posted code,

char ch;

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




  1. 假设字符串终止于一个NUL字节('\0')

  2. 假设 line [] 数组中有另一个字符的空间

  1. assuming that the string is terminated with a NUL byte ('\0')
  2. assuming there is room in the line[] array for another character

将成为:

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

这篇关于是否有类似于Java的字符串'charAt()'方法在C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆