繁体   English   中英

c 中的字符串连接问题

[英]strings concatenation issues in c

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
    int x,y;
    char s1[30],s2[30];
    printf("Enter string 1:");
    scanf("%s",s1);
    printf("Enter string 2:");
    scanf("%s",s2);
    x=sizeof(s1),y=sizeof(s2);
    char cs1[x+y+1];
    char cs2[x+y+1];
    for (int i = 0; i < x; i++)
    {
        cs1[i]=s1[i];
    }
    for (int i =x; i < (x+y); i++)
    {
        cs1[i]=s2[i];
    }
    for (int i = 0; i < (y); i++)
    {
        cs2[i]=s2[i];
    }
    for(int i=y;i<(x+y);i++)
    {
        cs2[i]=s1[i];
    }
    cs1[x+y]='\0';
    cs2[x+y]='\0';
    printf("string 1+string 2 is:%s",cs1);
    printf("\nstring 2+ string 1 :%s",cs2);
    
    
    
    

 





    return 0;
}

我试图在不使用内置函数的情况下连接两个字符串,你能指出错误吗? 这是给出字符串,我的意思是说它正在打印 s1 字符串代替 cs1 和 s2 代替 cs2

这些作业:

 x=sizeof(s1),y=sizeof(s2);

没有意义,因为分配的值不代表输入字符串的长度。

你需要写:

#include <string.h>

//...

size_t x,y;
char s1[30],s2[30];
printf("Enter string 1:");
scanf("%s",s1);
printf("Enter string 2:");
scanf("%s",s2);
x = strlen(s1), y = strlen(s2);
//...

这些 for 循环:

for (int i =x; i < (x+y); i++)
{
    cs1[i]=s2[i];
}

//...

for(int i=y;i<(x+y);i++)
{
    cs2[i]=s1[i];
}

不正确。 你需要写:

for ( size_t i = 0; i < y; i++)
{
    cs1[i + x] = s2[i];
}

//...

for( size_t i = 0; i < x; i++)
{
    cs2[i + y ] = s1[i];
}

试试这个,这对我有用,我创建了一个 function“my_strlen”来获取字符串的大小

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

    int my_strlen(char *str)
    {
        int i;

        for (i = 0; str[i] != '\0'; i++);
        return (i);
    }

    int main()
    {
        int x,y;
        char s1[30],s2[30];
        printf("Enter string 1:");
        scanf("%s",s1);
        printf("Enter string 2:");
        scanf("%s",s2);
        x=my_strlen(s1),y=my_strlen(s2);
        char cs1[x+y+1];
        char cs2[x+y+1];
        for (int i = 0; s1[i] != '\0'; i++)
        {
            cs1[i]=s1[i];
        }
        for (int i = 0; s2[i] != '\0'; i++)
        {
            cs1[i + x] = s2[i];
        }
        for (int i = 0; s2[i] != '\0'; i++)
        {
            cs2[i] = s2[i];
        }
        for(int i = 0; s1[i] != '\0'; i++)
        {
            cs2[i + y] = s1[i];
        }
        cs1[x+y]='\0';
        cs2[x+y]='\0';
        printf("string 1+string 2 is:%s",cs1);
        printf("\nstring 2+ string 1 :%s",cs2);
        return (0);
    }

x=sizeof(s1),y=sizeof(s2); sizeof 不会给你字符串的长度

请改用 function strlen

for循环也是错误的

    for (size_t i = 0; i < x; i++)
    {
        cs1[i]=s1[i];
    }
    for (int i = x; i < (x+y); i++)
    {
        cs1[i]=s2[i - x];
    }
    for (int i = 0; i < (y); i++)
    {
        cs2[i]=s2[i];
    }
    for(int i=y;i<(x+y);i++)
    {
        cs2[i]=s1[i - y];
    }

哦,好吧,就在我发布我的版本之前,一个答案被接受了,但只是为了参考,我还是会粘贴它。

正如其他人指出的那样,您的代码中存在许多错误:

  • sizeof(s)总是会产生 30,如果你想轻松计算字符串的长度,你必须使用自己定义的 function
  • 在各种for ,您在复制字符串时使用了错误的索引
  • 当我这样做时,我将scanf更改为fgets以便您也可以接受带空格的字符串。 我在顶部添加了一个#define MAX_LENGTH 30 ,这样您就可以轻松地重新调整程序的用途以获取更短/更长的输入

无论如何,这是代码

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

#define MAX_LENGTH 30   /* use defines instead of repeating the same number by hand */

int myStrLen(char *s, int max_len) {
  /* this little function counts the lenght of the passed string, up to max_len */
  int len = 0;
  for (int i = 0; i < max_len; i++) {
    if (s[i] != '\0' && s[i] != '\n') {
      len++;
    } else {
      break;
    }
  }
  return len;
}

int main() {
  int x, y;
  char s1[MAX_LENGTH], s2[MAX_LENGTH];

  printf("Enter string 1:");
  fgets(s1, MAX_LENGTH, stdin); /* using fgets instead of scanf allows reading strings containing spaces */
  //scanf("%s",s1);
  
  printf("Enter string 2:");
  fgets(s2, MAX_LENGTH, stdin);
  //scanf("%s",s2);

  x = myStrLen(s1, sizeof(s1));
  y = myStrLen(s2, sizeof(s2));
  printf("string1 length: %d\nstring2 length: %d\n", x, y);

  char cs1[x + y + 1];
  char cs2[x + y + 1];

  for (int i = 0; i < x; i++) {
    cs1[i] = s1[i];
  }
  for (int i = 0; i < y; i++) {
    cs1[i+x] = s2[i];   // nb: (i+x) instead of i
  }
  for (int i = 0; i < y; i++) {
    cs2[i] = s2[i];
  }
  for (int i = 0; i < x; i++) {
    cs2[i+y] = s1[i];   // // nb: (i+y) instead of i
  }

  cs1[x + y] = '\0';
  cs2[x + y] = '\0';

  printf("string1+string2 is:%s\n", cs1);
  printf("string2+string1 is:%s\n", cs2);
  return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM