鱼C论坛

 找回密码
 立即注册
查看: 2884|回复: 6

字符串长度存储问题

[复制链接]
发表于 2012-4-22 18:29:57 | 显示全部楼层 |阅读模式
1鱼币
#include<stdio.h>
#include<string.h>
#include<malloc.h>
void qwe(char T[],char* chars)//字符串的复制,从第二个位置复制,第一个位置用来存储长度
{
int i,len;
len = strlen(chars);
T=(char*)malloc(len * sizeof(char));

for(i = 1; i <= len ; i++)
{
T = chars[i-1];
}
T[0] = len;
printf("%d",T[0]);//这边可以输出
T = '\0';


}
void main()
{
char q[20];
char s[20];
printf("输入主串s:\n");
gets(s);
qwe(q,s);
printf("%d",q[0]);//为什么这边不能输出
}
我想利用字符串的第一个位置来存字符串的长度,但好像不行,也调试不出来,求高手帮忙

最佳答案

查看完整内容

#include #include //#include void qwe(char T[],char* chars) //字符串的复制,从第二个位置复制,第一个位置用来存储长度 { int i,len; len = strlen(chars); //strlen计算的不包括'\0' //T=(char*)malloc( (len+1) * sizeof(char));没必要分配内存空间 for(i = 1; i
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-4-22 18:29:58 | 显示全部楼层
#include<stdio.h>
#include<string.h>
                                                                //#include<malloc.h>
void qwe(char T[],char* chars)  //字符串的复制,从第二个位置复制,第一个位置用来存储长度
{
       
        int i,len;
        len = strlen(chars);                //strlen计算的不包括'\0'
                                                                //T=(char*)malloc( (len+1) * sizeof(char));没必要分配内存空间
        for(i = 1; i <= (len+1) ; i++)
        {
                T[i] = chars[i-1];                //T = chars[i-1]; 这应该是网站的问题
        }
        T[0] = len;
       
        printf("%d\n",T[0]);               
                                                                //        T = '\0';  
        /*                                                         
        int i,len;                                        //  这段代码可代替上面的,参考一下
        for(i = 1; *(chars+i-1) != '\0' ; i++)
        {
                T[i] = chars[i-1];       
        }
        T[0] = i-1;       
        printf("%d\n",T[0]);
        */
}
void main()
{
        char q[20+1];                //char q[20];如果第一个位置来存字符串的长度,那q长度就必须比s大1
        char s[20];
        printf("输入主串s:\n");
        gets(s);
        qwe(q,s);
        printf("%d\n",q[0]);  
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2012-4-22 21:35:23 | 显示全部楼层
简单看了一下,楼上正解
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2012-4-22 22:20:13 | 显示全部楼层
二楼说的对,不要把简单问题复杂化。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2012-4-23 09:28:43 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<string.h>

  3. #define MAX_CHARS 20
  4. void qwe(char* to, char* from);
  5. int main(void) {
  6.         char q[MAX_CHARS+1];
  7.         char s[MAX_CHARS];
  8.         printf("输入主串s:\n");
  9.         gets(s);
  10.         qwe(q,s);
  11.         printf("%d:%s\n",q[0],q+1);  
  12. }

  13. void qwe(char* to, char* from) {
  14.         to[0] = strlen( from );
  15.         strcpy( to+1, from );
  16. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-4-23 22:52:59 | 显示全部楼层
真的非常感谢您的详细解答,我觉得我爱上这个地方了,以前有问题都不知道问谁,感谢热心帮助他人的人
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-4-23 22:55:20 | 显示全部楼层

真的非常感谢您的详细解答,我觉得我爱上这个地方了,以前有问题都不知道问谁,感谢热心帮助他人的人
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-6 03:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表