Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的字符串_C - Fatal编程技术网

C语言中的字符串

C语言中的字符串,c,C,如果输出是这样的,你怎么能用C语言编写代码呢?我需要字符串的代码格式,因为我们的主题是字符串 #include <stdio.h> #include <stdlib.h> void main() { char my_string[50]; printf("Enter a word:"); scanf("%s", my_string); printf("Enter a word:"); scanf("%s", my_str

如果输出是这样的,你怎么能用C语言编写代码呢?我需要字符串的代码格式,因为我们的主题是字符串

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

void main() 
{ 
    char my_string[50];

    printf("Enter a word:");
    scanf("%s", my_string);

    printf("Enter a word:");
    scanf("%s", my_string);

    // Some unknown code here...
    // this part is my only problem to solve this.

    getch();
}

好吧,你需要做些调查。一般来说,我们不会为人们做家庭作业,因为:

  • 这是作弊
  • 如果你一字不差地抄袭,你可能会被发现
  • 从长远来看,这对你毫无帮助
应使用的用户输入的C库调用是
fgets
,大致如下:

char buffer[100];
fgets (buffer, sizeof(buffer), stdin);
这将向名为buffer的字符数组中输入一个字符串

如果使用两个不同的缓冲区执行此操作,则会将字符串存储在内存中

然后需要创建指向它们的指针,并遍历输出交替字符的两个字符串。指针不是一个简单的主题,但以下伪代码可能会有所帮助:

set p1 to address of first character in string s1
set p1 to address of first character in string s1
while contents of p1 are not end of string marker:
    output contents of p1
    add 1 to p1 (move to next character)
    if contents of p2 are not end of string marker:
        output contents of p2
        add 1 to p2 (move to next character)
while contents of p2 are not end of string marker:
    output contents of p2
    add 1 to p2 (move to next character)
将其翻译成C需要一些工作,但算法是可靠的。您只需要知道字符指针可以用
char*p1
,使用
*p1
获取其内容,并将其向前推进
p=p+1
p1++


除了为您编写代码(我不打算这么做),您可能不需要太多其他东西。

好的,这是您的代码。来吧,伙计们,如果他问这里,这意味着他不能解决这个问题

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

int main(int argc, char *argv[])
{
    char str1[] = "abcdefghijklmopq";
    char str2[] = "jklm";

    int len1 = strlen(str1);
    int len2 = strlen(str2);
    int c1 = 0, c2 = 0;
    int max = (len1 > len2) ? len1 : len2 ;

    char *result = malloc(len1 + len2);

    for(c1 = 0; c1 <= max; c1++) {
        if(c1 < len1)
            result[c2++] = str1[c1];

        if(c1 < len2)
            result[c2++] = str2[c1];
    }

    result[c2] = 0;

    printf("\n%s\n", result);

    return 0;
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
字符str1[]=“abcdefghijklmopq”;
字符str2[]=“jklm”;
int len1=strlen(str1);
int len2=strlen(str2);
int c1=0,c2=0;
int max=(len1>len2)?len1:len2;
char*result=malloc(len1+len2);
对于(c1=0;c1
#包括
#包括
#包括
void main()
{ 
char my_string1[50],my_string2[50];
int i,l1=1,l2=0;
printf(“输入一个单词:”);
scanf(“%s”,我的字符串1);
printf(“输入一个单词:”);
scanf(“%s”,我的字符串2);
l1=strlen(my_string1);/*第一根弦的长度*/
l2=strlen(my_string2);/*第二个字符串的长度*/
如果(l1==l2)
{

对于(i=0;i您可以看到,输出需要是一个字符串,其中逐个包含用户String1和用户String2的所有字符

你可以像这样做

//add #include<String.h>
int l1=strlen(s1);
int l2=strlen(s2);
if(l1!=l2)
{
    printf("length do not match");
    return 0;
}
char ansstr[l1+l2];
int i,j=0,k=0;
for(i=0;i<l1+l2;i=i+2)
{
    ansstr[i]=s1[j];
    ansstr[i+1]=s2[k];
    j++;
    k++;``
}
//ansstr is your answer
//添加#包括
int l1=strlen(s1);
int l2=strlen(s2);
如果(l1!=l2)
{
printf(“长度不匹配”);
返回0;
}
char-ansstr[l1+l2];
int i,j=0,k=0;

对于(i=0;闻起来像作业。标记为appropriate@Binary:看。还有:杰夫本人。我最近在那场辩论中错过了什么吗?@Skurmedel:应该读两个字符串并交错打印。请自己试试first@Binary当前位置我同意你的看法:毫无疑问,你会发现,向ho给出代码答案被认为是不好的形式mework问题:-)是的,但这是一个有用的答案吗如果获得选票所需要的只是一个答案,我会有一个远远超出Skeet目前所处位置的代表。@paxdiablo:不,但这是我所能看到的正确答案。不过你的答案更好,更具教育性。就我个人而言,我倾向于任何解释解决问题所采取步骤的答案,但在这个网站上只有很少的答案是h一个是黑胡子,我想有人在跟你闲聊:)LMAO。更严重的是,你要小心把你的电子邮件公之于众。很多人对你投了反对票。有些人可能会因为你回答这样的家庭作业问题而非常生气,他们会给你发电子邮件炸弹,或者给你注册大量的垃圾邮件——如果垃圾邮件发送者没有首先看到你的电子邮件地址,那就是说。有自动程序AMS的互联网搜索任何东西看起来像一个电子邮件地址。已经教C++多年,并赚钱我的金钱计划一起(希望)更长久以来,我完全同意所有投票反对你的人的看法。愿这个人有一天成为你的同事,整天坐在你的腿上需要你的帮助,因为他利用你这样的人在大学里作弊。
void main()

{

 char my_string1[50],my_string2[50];  int ptr;
 ptr=0;
 printf("Enter a word : ");
 scanf("%s",my_string1);
 printf("enter a word");
 scanf("%s",my_string2);

while(my_string1[ptr]!='\0' && my_string2[ptr]!='\0')
 {
 printf("%c%c",my_string1[ptr],my_string2[ptr]);
 ptr++;
 }


 if(my_string1[ptr]!='\0')
 {
  while(my_string1[ptr]!='\0')
   { printf("%c",my_string1[ptr]);
    ptr++;
   }
 }
else
{
  while(my_string2[ptr]!='\0')
 {printf("%c",my_string2[ptr]);
  ptr++;
  }

 }

 }
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
void main() 
{ 
       char my_string1[50],my_string2[50];
       int i,l1=1,l2=0;

       printf("Enter a word:");
       scanf("%s", my_string1);

       printf("Enter a word:");
       scanf("%s", my_string2);

       l1=strlen(my_string1); /* Length of 1st string */
       l2=strlen(my_string2); /* Length of 2nd string */
       if(l1==l2)
       {
            for(i=0;i<l1;i++)
            {
                printf("%c%c",my_string1[i],my_string2[i]);
            }
       }
       else
       {
                printf("Length of the entered strings do not match");
       }
}
//add #include<String.h>
int l1=strlen(s1);
int l2=strlen(s2);
if(l1!=l2)
{
    printf("length do not match");
    return 0;
}
char ansstr[l1+l2];
int i,j=0,k=0;
for(i=0;i<l1+l2;i=i+2)
{
    ansstr[i]=s1[j];
    ansstr[i+1]=s2[k];
    j++;
    k++;``
}
//ansstr is your answer