is there anything similar to Java's string 'charAt()' Method in C?

后端 未结 3 1837
清酒与你
清酒与你 2021-01-22 15:07

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          


        
3条回答
  •  伪装坚强ぢ
    2021-01-22 15:43

    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. assuming that the string is terminated with a NUL byte ('\0')
    2. assuming there is room in the line[] array for another character

    would become:

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

提交回复
热议问题