English 中文(简体)
取道
原标题:append chars to get a string
  • 时间:2009-12-23 18:49:05
  •  标签:
  • c
  • string

在C中,我有以下法典:

    char *str = "Hello World";
    char *s = malloc(strlen(str));
    int i =0;
    for(;i<strlen(str)-5;i++)
    {
        s += *(str+i);
    }
    printf(s);

这丝毫没有说明。 我想得到的是<条码>><>>>指示/编码>。 储存在<条码>上。

Java 我将采取以下行动:

    String str = "Hello World";
    String s="";

    for(int i=0;i<str.length()-5; i++)
        s+=str[i];

    System.out.println(s);

或者使用替代方法。 缩略语

我如何能够做到这一点?

最佳回答

使用<代码>strcpy功能。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[] ) 
{
  char *str = "Hello World";
  size_t length = strlen(str);

  char *s = (char*)malloc(sizeof(char) * (length + 1));

  strcpy(s, str);

  s[length] =   ; // makes sure it s NUL terminated

  printf("%s", s);

  free(s);

  return 0;
}

在分配目的地缓冲时,应注意下述事实:地壳由<条码>NUL终止。

仅复制次体,使用<代码>strncpy:

strncpy(s, str + 6, strlen(str) - 6);

仅将“世界”印成<条码>。

无论如何,在使用<代码>f等功能之前,确保终止本公约的编码。

另见strcatstrncat。 并且熟悉C阵列和点子。

问题回答

另一些人说,如何正确解决这一问题(特区),但骗局是思考类型。 C do ANYTHING magical for You. 页: 1 你在char中增加3个吗? 你们还有另一个char* ,它指三个特征,距离线更远。 添加一个果园*与将97(a)项的(a)项的(a)项)添加到点子一样,从而指明了另一类,大大缩小了阵列......

我希望能解释所发生的事情。

您可在<条码>+=上使用<条码>。 反对——它只是一个点来<条码>。 相反,你希望使用strncpy,并通过该密码作为预先分配的缓冲,加上一个点子,说明你想要开始复制,以及你想要复制的特性。

在一般情况下,如果你想要从一开始和最后都放弃特性,那么你就应当使用str或str。 (尽管有谨慎,但体力不一定是NUL-terminate)。)

使用<代码>strncpy():

char *str = "Hello World";         
size_t len = strlen(str) - 5;   
char *s = malloc(len + 1); // +1 for nul terminator
if (s)            
{
  strncpy(s, str, len); // copies strlen(str)-5 characters from str to s
  s[len] = 0; // add nul terminator
}

更一般性的职能:

/**
 * Return a substring of _len_ characters starting at position _start_
 * substring("Hello, World", 2, 3) == "llo"
 */
char *substring(char *str, size_t start, size_t len)
{
  char *s = malloc(len + 1);
  if (s)
  {
    strncpy(s, str+start, len);
    s[len] = 0;
  }
  return s;
}

视需要增加疗效(例如,开端和提炼;透镜、透镜、透镜等);

你们需要为留级的扼杀活动分配记忆。 也许可以这样说:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define LESS 4
#define MAX 10 /*Keep it atleast strlen(str) - LESS + 1*/

int main(int argc, char *argv[] )
{

        char *str = "Hello World";
        char s[MAX]; /* Declare the array to hold the substring.*/
        int i =0,j=0; /* i is index into original string and j in the result string. */
        for(;i<strlen(str)-LESS;i++)
        {
                s[j++] = *(str+i);
        }
        s[j] =   ; /* Terminate the result string.*/
        printf("%s
",s); /* Output: Hello W */

        return 0;
}

你可以做你要求做的修改,修改你的法典如下:

int main (int argc, char *argv[])
{
    char *str = "Hello World";
    char *s = malloc(strlen(str));
    int i = 0;
    for(;i<strlen(str)-5;i++)
    {
        s[i] = *(str+i);
    }
    s[i] = 0;

    printf(s);

   return 0;
}

+ 不是C的固定组别经营者





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...