string可以获得一个int变量吗? [英] Does string can get an int variable?

查看:197
本文介绍了string可以获得一个int变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我试图计算并显示字符串中的相同字符

当我完成计数时我想输入一个新字符串但我得到的数字垃圾代替

请帮助,thx(:(从最后开始的第三行)

[ ]

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define N 100

void doArchive (char * str, char * dest);

void main()
{
    char str[N],dest[N];
    printf("Enter your string : ");
    gets(str);
    doArchive(str,dest);

}
void doArchive(char * str, char * dest)
{
    int sc,j,dc,numOfStr=strlen(str),count=0;;
    for(sc=0,dc=0;sc<numOfStr;sc++)

    {

        dest[dc]=str[sc];

        for(j=0;str[sc]==str[j];j++)

        {

            count++;

            if(count>9)
            {
                dest[dc+1]='9';
                dest[dc+2]=dest[dc];
                dc+=3;
                count=1;
            }
        }
        dest[dc+1]=count;
        dc+=2;
        sc=j;

    }

}

推荐答案

你的问题和你的代码给了我不同的关于你可能想要什么的想法:(

A)如果你想打印到字符串而不是标准输出,请使用 sprintf (请参阅: http://www.rohitab.com/discuss/topic/11505-sprintf -tutorial-in-c / [ ^ ]。

B)在您的情况下,dest数组只能用于存储计数,但我会使用int *而不是char *。但是你不能直接打印它。你需要一个循环打印计数作为该循环内的数字。



但我们不做作业,我们没有它的规格 - 继续,试着找出你想念的东西。



[更新]

好​​的,所以这是一个RLE,即使不是最好的。但是看到这个解决方案,并附上一些评论如果是C,那就是C.



Your question and your code gave me different ideas about what you probably want :(
A) If you want to "print" to a string instead of the standard output, use sprintf (see: http://www.rohitab.com/discuss/topic/11505-sprintf-tutorial-in-c/[^]).
B) In your case dest array can be only used to store the count, but I would use int * instead of char *. But you can not print it directly. You will need a loop print the counts as numbers from within that loop.

But we don''t do homework, we don''t have the specification of it - thus go on, and try to figure out, what you missed.

[Update]
Ok, so it is an RLE, even not the finest one. But see this solution, with some comment. If C, make it C.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100

char *doArchive(char *str, char *dest)
{
    char tmp[10]; // needed to store intermediate results, actually should be log10(N)+2
    int idx_from;

    if(!str || !dest || !str[0]) return NULL; // check input

    *dest = 0; //make an empty result
    idx_from = 0; // start at the beginning
    do
    {
        int idx_to = idx_from + 1; // look ahead
        while(str[idx_to] && str[idx_from] == str[idx_to]) idx_to++; // find the next that does not match, or teh end of the string

        sprintf(tmp, "%c%d", str[idx_from], idx_to-idx_from); // duild the sequence code
        strcat(dest, tmp); // add it to the result

        idx_from = idx_to; // go to the next sequence
    }
    while(str[idx_from]); // until we reach the end of the string

    return dest;
}

void main()
{
    char str[N]; //the input string
    char dest[N*2]; //the longest if every character is once
    printf("Enter your string: ");
    gets(str);

    printf("Result: %s", doArchive(str,dest));
}


你可能想写:



You probably wanted to write:

dest[dc+1] = ''0'' + count;


这篇关于string可以获得一个int变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆