English 中文(简体)
2. 在C.中进行无剧的分类
原标题:String concatenation without strcat in C
  • 时间:2010-12-14 11:15:20
  •  标签:
  • c
  • string

我在C地块混凝土,没有图书馆功能。 我的法典

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

int main()
{
  char *a1=(char*)malloc(100);
  strcpy(a1,"Vivek");
  char *b1=(char*)malloc(100);
  strcpy(b1,"Ratnavel");
  int i;
  int len=strlen(a1);

  for(i=0;i<strlen(b1);i++)
  {
     a1[i+len]=b1[i];
  }

  a1[i+len]=  ;                
  printf("

 A: %s",a1);

  return 0;
}

我对守则做了更正。 这一工作正在进行。 现在,我怎么做呢?

最佳回答

旧的答案如下:


你可以像你的法典那样,以“<条码>斯特佩克/代码”为首,或直接在申报地毯时。

char a1[100] = "Vivek";

除此以外,你还可以做事。

a1[0] =  V ;
a1[1] =  i ;
// ...
a1[4] =  k ;
a1[5] =   ;

或者,你可以写出几条法典,取代<条码>strcpy,使之成为你的主要职务或直接使用。


Old answer

        0 1 2 3 4 5 6 7 8 9 ...
    a1 [V|i|v|e|k|0|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_]
    b1 [R|a|t|n|a|v|e|l|0|_|_|_|_|_|_|_|_|_|_|_|_|_]

        0 1 2 3 4 5 6 7 8 9 ...
    a1 [V|i|v|e|k|R|a|t|n|a|v|e|l|0|_|_|_|_|_|_|_|_]

......

a1[5] =  R ;
a1[6] =  a ;
// ...
a1[12] =  l ;
a1[13] =   ;

但有 lo和 st,右面吗?

引证(增加缺失的借方)

for (aindex = 5; aindex < 14; aindex++) {
    a1[aindex] = b1[aindex - 5];
}

现在我想到的是以上案文中的<代码>5和14

你们可以以什么方式取代这些内容? 在你回答这个问题时,你解决了你的方案拟订问题:

问题回答
 char a1[] = "Vivek";

将为大小代码(<6)设定一个阵列。 你试图以比它能够承受的更重的特性来加以扼杀。

如果你想能够适应分类<代码>“Vivek”和“Ratnavel”,那么你需要拥有一个小数体大小的星体,即<代码>14(5+8 + 1)。

在您的<>经修改的方案中,你正在做以下工作:

char *a1=(char*)malloc(100);  // 1
a1 = "Vivek";                 // 2

1: Will allocate a memory chunk of size 100 bytes, makes a1 point to it.
2: Will make a1 point to the string literal "Vivek". This string literal cannot be modified.

2. 将该插图填入所分配的记忆中:

char *a1=(char*)malloc(100);  
strcpy(a1,"Vivek");

另外,< <<>条码>>路况<条码>i<strlen(b1)-1<条码>将不复制地体内的最后特性,将其改为<条码>i<strlen(b1)

And

a1[i]=  ;

应当

a1[i + len]=  ;

由于<代码>a1的新长度为i+len,你需要将NUL的特性列入该指数。

并且不会忘记<条码>免费<>。

你不能安全地写进这些阵列,因为你没有确保有足够的空间。 如果您使用<条码>(<>/条码>来分配空间,则你可以通过分配字面来推翻点名。 在此情况下,你需要使用<代码>strcpy()复制一个插图,以复制新分配的缓冲器。

此外,C号插座的长度由以下功能计算:strlen(>功能,而不是length(,经您重新使用。

在召集时,你需要在适当的地点终止,你的法典似乎并没有这样做。

这里,如果出于某种原因需要,我将如何重新实施<条码>>strcat():

char * my_strcat(char *out, const char *in)
{
  char *anchor = out;
  size_t olen;

  if(out == NULL || in == NULL)
    return NULL;

  olen = strlen(out);
  out += olen;
  while(*out++ = *in++)
    ;
  return anchor;
}

Note that this is just as bad as strcat() when it comes to buffer overruns, since it doesn t support limiting the space used in the output, it just assumes that there is enough space available.

问题:

  1. length isn t a function. strlen is, but you probably shouldn t call it in a loop - b1 s length won t change on us, will it? Also, it returns a size_t, which may be the same size as int on your platform but will be unsigned. This can (but usually won t) cause errors, but you should do it right anyway.
  2. a1 only has enough space for the first string, because the compiler doesn t know to allocate extra stack space for the rest of the string since. If you provide an explicit size, like [100], that should be enough for your purposes. If you need robust code that doesn t make assumptions about what is "enough", you should look into malloc and friends, though that may be a lesson for another day.
  3. Your loop stops too early. i < b1_len (assuming you have a variable, b1_len, that was set to the length of b1 before the loop began) would be sufficient - strlen doesn t count the at the end.
  4. But speaking of counting the at the end, a slightly more efficient implementation could use sizeof a1 - 1 instead of strlen(a1) in this case, since a1 (and b1) are declared as arrays, not pointers. It s your choice, but remember that sizeof won t work for pointers, so don t get them mixed up.
    EDIT: New problems:
  5. char *p = malloc(/*some*/); p = /* something */ is a problem. = with pointers doesn t copy contents, it copies the value, so you re throwing away the old pointer value you got from malloc. To copy the contents of a string into a char * (or a char [] for that matter) you d need to use strcpy, strncpy, or (my preference) memcpy. (Or just a loop, but that s rather silly. Then again, it may be good practice if you re writing your own strcat.)
  6. Unless you re using C++, I wouldn t cast the return value of malloc, but that s a religious war and we don t need one of those.
  7. 如果有<代码>strdup,则使用。 如果你不这样做的话,这里是工作执行:

    char *strdup(const char *c)
    {
        size_t l = strlen(c);
        char *d = malloc(l + 1);
        if(d) memcpy(d, c, l + 1);
        return d;
    }
    

    It is one of the most useful functions not in the C standard library.

你们也可以使用大体(也)这样做;

char *a = (char *) malloc(100);
char *b = (char *) malloc(100);

strcpy(a, "abc");               // initializes a
strcpy(b, "def");               // and b

strcpy((a + strlen(a)), b);     // copy b at end of a

printf("%s
",a);               // will produce: "abcdef"

i 认为这是一个容易的事情。

#include<stdio.h>
int xstrlen(char *);
void xstrcat(char *,char *,int);
void main()
{
    char source[]="Sarker";
    char target[30]="Maruf";
    int j=xstrlen(target);
    xstrcat(target,source,j);
    printf("Source String: %s
Target String: %s",source,target);
}
int xstrlen(char *s)
{
    int len=0;
    while(*s!=  )
    {
        len++;
        s++;
    }
    return len;
}
void xstrcat(char *t,char *s,int j)
{
    while(*t!=  )
    {
        *t=*t;
        t++;
    }
    while(*s!=  )
    {
        *t=*s;
        s++;
        t++;
    }
}

最好把<代码>strcat逻辑与单独职能挂钩。 如果你使用点算法,你不需要<条码>。

#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* To completely get rid of this, 
                       implement your our strcpy as well */

static void 
my_strcat (char* dest, char* src)
{
  while (*dest) ++dest;
  while (*src) *(dest++) = *(src++);
  *dest = 0;
}

int 
main()
{
  char* a1 = malloc(100);
  char* b1 = malloc(100);

  strcpy (a1, "Vivek");  
  strcpy (b1, " Ratnavel");

  my_strcat (a1, b1);
  printf ("%s
", a1); /* => Vivek Ratnavel */

  free (a1);
  free (b1);
  return 0;
}




相关问题
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 "...

热门标签