Click here to Skip to main content
15,891,744 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello
im trying to count and show the same chars that in the string
when im done to count i want enter the number to a new string but i get garbage instead
pls help,thx (: (third row from the end)
[]
XML
#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;

    }

}
Posted
Updated 1-Jan-13 5:36am
v3
Comments
Richard MacCutchan 1-Jan-13 12:31pm    
You are trying to store an integer into a character variable, which will not give you a valid result.

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.

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));
}
 
Share this answer
 
v3
Comments
idobry 1-Jan-13 12:38pm    
this is what im trying to do:
string1: AAAA##%
new string will be: A4#2%1
Zoltán Zörgő 1-Jan-13 12:44pm    
Use option A, sprintf!
Zoltán Zörgő 1-Jan-13 15:23pm    
See update
Sergey Alexandrovich Kryukov 1-Jan-13 13:33pm    
Reasonable, my 5. OP poorly understands the topic, so the simple question looks pretty vague...
—SA
idobry 2-Jan-13 3:01am    
thx for this code! working very good accept i need to show if there's a 9X of the same chars but i will use your example wisely and learned from it.
You probably wanted to write:

dest[dc+1] = '0' + count;
 
Share this answer
 
Comments
Zoltán Zörgő 1-Jan-13 12:36pm    
And what if the count is 99? '0'+99 = 48+99 = 147 = 'g' !
nv3 1-Jan-13 14:09pm    
As you can see, he limited count to be <= 9 in his code. If there are more than 9 characters of the same type in a row, he wants to output x9 and then start over counting. That's what is done inside the "if count <9" clause. Don't ask my why, but this is what I try to gues from his code. If he just wanted to output a decimal number, your approach with printf would be obviously the best choice. But it didn't seem to be what OP wanted to do.
idobry 1-Jan-13 12:38pm    
didnt worked ):
nv3 1-Jan-13 14:10pm    
You also forgot to terminate your destination string with a '\0'. And if you say "it didn't work" what was your program doing, and what were you expecting instead?
idobry 1-Jan-13 14:20pm    
About the count>9 you got it right.
this is what im trying to do:
string1: AAAA##%
new string will be: A4#2%1
and with what you wrote me the string didnt got the count(if you look in the example-the new string didnt got the num 4)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900