Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.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中的int串联_C_String_Int_Strcat - Fatal编程技术网

将字符串与C中的int串联

将字符串与C中的int串联,c,string,int,strcat,C,String,Int,Strcat,} 这是我的一段代码,函数得到一组字符,即:aaabbccc,它假设计算每个字符被输入的次数,然后它将字母发送到另一个字符串,然后我想将计数器的值连接到shortText[j],然后程序崩溃。erorr列表中给出的错误为“strcat”:形式参数和实际参数1/2的类型不同 有什么方法可以让它工作呢?两种可能的方法。对于案例aaabb: 如果您想要a3b2: #include <stdio.h> #include <string.h> int main(void) {

} 这是我的一段代码,函数得到一组字符,即:aaabbccc,它假设计算每个字符被输入的次数,然后它将字母发送到另一个字符串,然后我想将计数器的值连接到shortText[j],然后程序崩溃。erorr列表中给出的错误为“strcat”:形式参数和实际参数1/2的类型不同
有什么方法可以让它工作呢?

两种可能的方法。对于案例aaabb:

如果您想要a3b2:

#include <stdio.h>
#include <string.h>
int main(void)
{
    //declaring variables
    int num[10], action;
    char longText[80], shortText[80] = { 0 };

    //Program's Main menu
    printf("You can use the following options:\nPlease select your desired action.\n");
    scanf("%d", &action);
    switch (action) //All of the possible options for the user
    {
        case 1:
        {
            switchIf(num);
            break;
        }
        case 2:
        {
            newNumbers(num);
            break;
        }
        case 3: 
        {
            longToShortText(longText, shortText);
            break;
        }
    }
}
void longToShortText(char longText[], char shortText[])
{
    int i, j = 0, counter = 1, size;
    printf("Please enter a series of letters.\n");
    scanf("%s", longText);
    size = strlen(longText);
    for (i = 0; i < size; ++i)
    {
    if (longText[i] == longText[i + 1])
        ++counter;
    else
    {
        shortText[j] = longText[i];
        strcat(shortText[j], counter); // i know this is wrong, but i couldn't make it work with another string either.
        printf("%c", shortText[j]);
        counter = 1;
        ++j;
    }
}
如果需要\0\0\0\3b\0\0\0\0\2示例假定整数为4字节

shortText[j] = longText[i];
++j;
char aux[20];
sprintf(aux, "%d", counter);
printf("aux: %s, j: %d\n", aux, j);
strcat(shortText+j, aux);
counter = 1;
j += strlen(aux);
在第二种情况下要小心,因为您将无法再使用字符串函数,如strcat、strcpy等

我今天尝试过

参考下面我在那里回答的代码,检查它是否对您有帮助

代码的输出是a3b2d4

注意:如果计数超过a,则可能需要将大小从2增加 字符大小


它不起作用或类似的问题描述永远不够好,请更具体一些。发生了什么,起了什么作用,应该发生什么,如果有错误消息,请给出错误消息,并将代码减少到最低限度以再现问题。sprintftarget\u str,%s%d,string\u part,counter,但最好使用snprintf以避免溢出。请:LongtosortTextChar longText[],char shortText[]。字符数组衰减为指针,函数无法知道数组有多大,将其更改为longToShortTextchar*longText,char*shortText,size\t long\u len,size\t short\u len,并将数组的大小传递给函数您希望aaabb的shortText中有什么内容您想要a3b2吗?还是\0\0\0\3b\0\0\0\0\2?如果您想要第一个问题,只需使用sprintf将int转换为char*,然后在第二个案例中使用strcat,您应该使用memcpyi如果您认为发现了重复的问题,请将其标记为重复的问题,而不是发布重复的答案。@ChrisF。。好的,回答是因为问题不完全重复。。但最终的答案在我看来是一样的!
shortText[j] = longText[i];
++j;
memcpy(shortText+j, &counter, sizeof(counter));
printf("%c, %d, %d, %d\n", shortText[j-1], counter, sizeof(counter), j);
counter = 1;
j += sizeof(counter); 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LONG 80
#define SHORT 20
void longtext(char longtxt[LONG], char shorttxt[SHORT])
{
    int i, j=0, count=0, tmp;
    char letter;
    for (i = 0; i <= strlen(longtxt); ++i)
    {
        if (i == 0)
        {
            letter = longtxt[i];
            ++count;
        }
        else if (letter == longtxt[i])
            ++count;
        else
        {
            shorttxt[j] = letter;
            snprintf(&shorttxt[j + 1],2,"%d",count);
            j += 2;
            count = 1;
            letter = longtxt[i];
        }
    }
}
int main()
{
    char longtxt[LONG] = "aaabbdddd",shorttxt[SHORT];
    longtext(longtxt,shorttxt);
    printf("%s", shorttxt);
}