【问题标题】:Print (work with) substrings in a C program在 C 程序中打印(使用)子字符串
【发布时间】:2021-05-02 06:17:18
【问题描述】:

如果我只想打印字符串的一部分,我该如何处理它?

例如,从"abcdef"打印"bcd"

我知道如何使用指向字符串开头的指针,但我不知道如何确定结尾。

void vypis(char *retezec)
{   
    printf("%s", retezec);
}

int main (void)
{
    char *str = NULL;
    size_t capacity;
        
    getline(&str, &capacity, stdin);    
    vypis(str);
    vypis(str+1);       
}

【问题讨论】:

标签: c printf substring c-strings function-definition


【解决方案1】:

您可以将不想打印的第一个字符(在您的示例中为 e)临时设置为 \0

void vypis(char *retezec, size_t delka){
    char aux = retezec[delka];
    retezec[delka] = '\0';
    printf("%s", retezec);
    retezec[delka] = aux;
}

为了确保它也适用于字符串文字和其他 const char 指针:

void vypis(const char* str, size_t delka){
    char aux = retezec[delka];
    char* retezec = (char*) str;
    retezec[delka] = '\0';
    printf("%s", retezec);
    retezec[delka] = aux;
}

【讨论】:

  • 注意,这不适用于字符串文字:vypis("hello", 2);
  • @JohnnyMopp 为什么永远不要将字符串文字传递给接受char* 而不是const char* 的函数。
  • 谢谢,我只想问你为什么要在那儿加1,给列表比我想要的多一个字符串?
  • @Aaron7 这是一个糟糕的解决方案,因为您无法将这些函数与字符串文字一起使用。任何更改字符串文字的尝试都会导致未定义的行为。
【解决方案2】:

我知道如何使用指向字符串开头的指针,但我不知道如何确定结尾。

一个指向您要打印的最后一个字符的指针是一种可能的解决方案:

void vypis(const char *retezec, const char *end)
{   
    while (retezec <= end && *retezec != '\0')
    {
        putchar(*retezec);
        retezec++;
    }
    putchar('\n');
}

int main (void)
{
    char *str = NULL;
    size_t capacity = 0;
        
    getline(&str, &capacity, stdin);    
    vypis(str, str + 5);      //prints from str[0] to str[5]
    vypis(str + 1, str + 3);  //prints from str[1] to str[3]     
}

【讨论】:

    【解决方案3】:

    你来了。

    #include <stdio.h>
    
    int main(void) 
    {
        char *s = "abcdef";
        size_t pos = 1;
        int n = 3;
        
        printf( "%.*s\n", n, s + pos );
        
        return 0;
    }
    

    程序输出是

    bcd
    

    或者另一个例子

    #include <stdio.h>
    #include <string.h>
    
    int main(void) 
    {
        char *s = "abcdef";
    
        for ( int i = 0, n = ( int )strlen( s ); i < n; i++ )
        {
            printf( "%.*s\n", i + 1, s );
        }
        
        return 0;
    }
    

    程序输出是

    a
    ab
    abc
    abcd
    abcde
    abcdef
    

    您可以编写一个通用函数,该函数可以在任何流中输出子字符串,例如在打开的文件中。

    #include <stdio.h>
    #include <string.h>
    
    FILE * print_substring( const char *s, size_t pos, size_t n, FILE * fp )
    {
        size_t len = strlen( s );
        
        if ( pos < len )
        {
            if ( len - pos < n ) n = len - pos;
            
            fprintf( fp, "%.*s", ( int )n, s + pos );
        }
        
        return fp;
    }
    
    int main(void) 
    {
        char *s = "abcdef";
    
        for ( int i = 0, n = ( int )strlen( s ); i < n; i++ )
        {
            putc( '\n', print_substring( s, 0, i + 1, stdout ) );
        }
        
        return 0;
    }
    

    程序输出是

    a
    ab
    abc
    abcd
    abcde
    abcdef
    

    【讨论】:

      【解决方案4】:

      你可以试试这个:

      #include <stdio.h>
      #include <string.h>
      
      void printSubString(const char *str, int start, int end) {
          int length = strlen(str);
          if (start >= length) {
              return;
          }
          if (end >= length) {
              return;
          }
          int index = start;
          while(index <= end) {
              printf("%c", str[index++]);
          }
          printf("\n");
      }
      
      int main() {
          char *str = "Hello, world!";
          printSubString(str, 3, 10);
          return 0;
      }
      

      输出:

      lo, worl
      

      【讨论】:

        猜你喜欢
        • 2019-03-01
        • 1970-01-01
        • 2021-03-30
        • 1970-01-01
        • 1970-01-01
        • 2015-09-04
        • 2015-02-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多