Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
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)
[]
#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 1-Jan-13 5:27am
idobry388
Edited 1-Jan-13 5:36am
v3
Comments
Richard MacCutchan at 1-Jan-13 12:31pm
   
You are trying to store an integer into a character variable, which will not give you a valid result.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Your question and your code gave me different ideas about what you probably want Frown | :(
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));
}
  Permalink  
v3
Comments
idobry at 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ő at 1-Jan-13 12:44pm
   
Use option A, sprintf!
Zoltán Zörgő at 1-Jan-13 15:23pm
   
See update
Sergey Alexandrovich Kryukov at 1-Jan-13 13:33pm
   
Reasonable, my 5. OP poorly understands the topic, so the simple question looks pretty vague...
—SA
idobry at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You probably wanted to write:
 
    dest[dc+1] = '0' + count;
  Permalink  
Comments
Zoltán Zörgő at 1-Jan-13 12:36pm
   
And what if the count is 99? '0'+99 = 48+99 = 147 = 'g' !
nv3 at 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 at 1-Jan-13 12:38pm
   
didnt worked ):
nv3 at 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 at 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)
nv3 at 1-Jan-13 16:10pm
   
So which result did you get then? And why don't you run your program in a debugger and see what is wrong?

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 345
1 _Amy 210
2 Peter Leow 175
3 Nirav Prabtani 167
4 Andreas Gieriet 160
0 OriginalGriff 7,540
1 Sergey Alexandrovich Kryukov 6,597
2 Maciej Los 3,849
3 Peter Leow 3,653
4 CHill60 2,712


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100