0

I wanted to write a program which counts the occurrences of each letter in a string, then prints one of each letter followed by the count for that letter.

For example:

aabbcccd - Has 2 a, 2 b, 3 c, and 1 d

So I'd like to convert and print this as:

a2b2c3d1

I wrote code (see below) to perform this count/conversion but for some reason I'm not seeing any output.

#include<stdio.h>
main()
{
    char array[]="aabbcccd";
    char type,*count,*cp=array;
    while(cp!='\0'){
      type=*cp;
      cp++;
      count=cp;
      int c;
      for(c=1;*cp==type;c++,cp++);
      *count='0'+c;
    }
    count++;   
    *count='\0';
    printf("%s",array);
}

Can anyone help me understand why I'm not seeing any output from printf()?

3
  • 1
    Nonworking code is not a question.
    – djechlin
    Commented Oct 23, 2012 at 14:10
  • There's an obvious question here. Why didn't you edit the post to include it, instead of closing? :)
    – hobbs
    Commented Oct 23, 2012 at 20:51
  • agreed with @hobbs, this has a clear programming question to it. I Edited it to show what that was, I think it should be reopened.
    – Mike
    Commented Oct 26, 2012 at 14:59

5 Answers 5

3
char array[]="aabbcccd";
char type,*count,*cp=array;
while(cp!='\0'){ 

*cp is a pointer it's pointing to the address of the start of the array, it will never be == to a char '\0' so it can't leave the loop.

You need to deference the pointer to get what it's pointing at:

while(*cp != '\0') {
...

Also, you have a ; after your for loop, skipping the contents of it:

for(c=1;*cp==type;c++,cp++); <-- this ; makes it not execute the code beneath it

After fixing both of those problems the code produces an output:

mike@linux-4puc:~> ./a.out 
a1b1c2cd

Not the one you wanted yet, but that fixes your problems with "printf not functional"

3
  • Well, actually it loops forever :)
    – hobbs
    Commented Oct 23, 2012 at 14:12
  • I need to point out making it "do nothing" was the original intent; I cluttered every in the header of for loop
    – EthOmus
    Commented Oct 23, 2012 at 14:21
  • @EthOmus - got it, well you can leave that ; in in that case, nice to leave a comment so one doesn't assume it was a mistake
    – Mike
    Commented Oct 23, 2012 at 14:25
3

Incidentally, this code has a few other major problems:

  1. You try to write past the end of the string if the last character appears once (you write a '1' where the trailing '\0' was, and a '\0' one character beyond that.
  2. Your code doesn't work if a character appears more than 9 times ('0' + 10 is ':').
  3. Your code doesn't work if a character appears more than 2 times ("dddd" doesn't become "d4"; it becomes "d4dd").
1

Probably line-buffering. Add a \n to your printf() formatting string. Also your code is very scary, what happens if there are more than 9 of the same character in a row?

0

1) error correction

while(*cp!='\0'){

and not

while(cp!='\0'){

2) advice

do not use array[] to put in your result user another array to put in your rusel it's more proper and eay

0

I tried to solve your question quickly and this is my code:

#include <stdio.h>

#define SIZE 255

int main()
{
  char input[SIZE] = "aabbcccd";/*input string*/
  char output[SIZE]={'\0'};/*where output string is stored*/
  char seen[SIZE]={'\0'};/*store all chars already counted*/
  char *ip = input;/*input pointer=ip*/
  char *op = output;/*output pointer = op*/
  char *sp = seen;/*seen pointer=sp*/
  char c,count;
  int i,j,done;

  i=0;
  while(i<SIZE && input[i]!='\0')
  {
    c=input[i];
    //don't count if already searched:
    done=0;
    j=0;
    while(j<SIZE)
    {
      if(c==seen[j])
      {
         done=1;
         break;
      }
      j++;
    }
    if(done==0)
    {//if i never searched char 'c':
      *sp=c;
      sp++;
      *sp='\0';
      //count how many "c" there are into input array:
      count = '0';
      j=0;
      while(j<SIZE)
      {
         if(ip[j]==c)
         {
        count++;
         }
     j++;
      }
      *op=c;
      op++;
      *op=count;
      op++;
    }
    i++;
  }

  *op='\0';
  printf("input: %s\n",input);
  printf("output: %s\n",output);

  return 0;
}

It's not a good code for several reasons(I don't check arrays size writing new elements, I could stop searches at first empty item, and so on...) but you could think about it as a "start point" and improve it. You could take a look at standard library to copy substring elements and so on(i.e. strncpy).

Not the answer you're looking for? Browse other questions tagged or ask your own question.