C51字符串连接

字符串以二维数组的形式存放在ROM内。如下:
char code tab[320][6]={
"aa001","aa002","aa003",………………}
连接函数如下:
char str_temp[15]; //必须要作为一个全局变量,因为要传递给输出函数使用
char *StrC(char *str1,char *str2,char *str3)
{
int i;
for (i=0;i<5;i++)
{ str_temp[i]=*(str1+i);}
for (i=0;i<5;i++)
{ str_temp[5+i]=*(str2+i);}
for (i=0;i<5;i++)
{ str_temp[10+i]=*(str3+i);}
return str_temp;
}

其他的方法:
#include<string.h>
char *StrCatenate(char *str1,char *str2)
{
return ( strcat( str1,str2 ) );
}
虽然这样嫩很简单的连接字符串,并且上位机显示也是正常的,但是如果发给LED控制器的时候实际上第一个字符串末尾的'\0'也会被作为一个字符显示,结果为乱码。

其他参考:
1、
C 里面的字符串是谙数组形式存储的。在内存中,字符串是一快连续的内存单元的以‘\0’结尾的字符集。 因此,要把 A 一个字符接在 B 后面时,必须先找到 A 的末尾。再把 B 的元素一个接一个的赋过去。
// 找到 A 的末尾。   
for( i = 0; A[i] != '\0'; i++ )    ;   
for( j = 0; B[j] != '\0'; j++) // 链接
A[i+j] = B[j];
A[i+j] = '\0' ; // 字符串结尾  
2、
#include<stdio.h>
#include<string.h>

int main(void)
{
     char a[10]="abcde",b[]="gfhi";
     int i=0,j=0;
     j=strlen(a);
     while(b[i]!='\0')
        a[j++]=b[i++];
     a[j]='\0';
     puts(a);
     getch();
     return 0;
}
3、该部分从以下地址引用http://cid-fa13e949968b9621.spaces.live.com/blog/cns!FA13E949968B9621!126.trak
C语言—-字符串连接

strcat and strncat are library functions that perform string CONCATENATION. (require string.h) They require dynamically allocating memory before the concatenation of strings.

Concatenating Strings Using
strcat

strcat takes two char * arguments and returns the concatenated string as a char *. Here's a simple use of strcat:

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

int main() {
char str1[50] = "Hello ";
char str2[] = "World";

strcat(str1, str2);
printf("str1: %s\n", str1);
return 0;
}

Output:

1
str1: Hello World

This only works if you've defined the str1 array to be large enough to hold the characters of your string. If you don't specify a size, the program may crash.

In some cases, you'd probably want to use char * variables. To perform a successful concatenation without the program crashing or complaining about memory, it's best to calculate and allocate enough memory to hold the resulting string. You can do this with either calloc or malloc.

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

int main() {
char *str1 = "Hello ";
char *str2 = "World";
char *str3;

str3 = (char *)malloc((strlen(str1) + strlen(str2) + 1)
*sizeof(char));
strcpy(str3, str1);
strcat(str3, str2);

printf("str3: %s\n", str3);

free(str3);

return 0;
}

The output in both cases is:

1
str3: Hello World

The amount of memory to allocate is worked out by finding the lengths of str1 and str2 and adding 1 (to allow for the NULL character). Concatenating Strings Using strncat

This function's almost the same as strcat – it takes a third attribute, which is the number of characters to concatenate from the source string.

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

int main() {
char *str1 = "It is ";
char *str2 = "raining sunny snowing foggy";
char *str3;

str3 = (char *)calloc(strlen(str1) + strlen(str2), sizeof(char));

strcpy(str3, str1);
strncat(str3, str2+8, 5);

printf("str3: %s\n", str3);

free(str3);

return 0;
}

Output:

1
str3: It is sunny

分享到: 更多
版权申明:

本站保留所有原创文章的版权,本站地址:奔跑的博客[http://www.elecbench.com]

原创文章转载时请注明出处,并添加文章所在页面的链接:http://www.elecbench.com/28/

本站所有 2010年3月4日 以后发表、未标明为“转载”的文章均是本站原创。

发表评论


(设置自己的个性头像)

*

申请属于你的免费顶级域名