Count how many times each character appears in a string (without using a function)

-2

I have the doubt in a question of string exercises that I am doing. The question is this below:

  

Write a program that reads a string and prints how many times each   character

     

appears in this string

     

1st string:

TTAAC
     

Result:

T: 2X
A: 2X
C: 1X

Remembering that it can not be done using any function.

    
asked by anonymous 26.03.2018 / 06:02

3 answers

-1

Code finalized !! EDITED

#include <stdio.h>

main(){
    char str[30];
    int i, j, count = 0, aux = 0, c = 1;

    puts("Informe a String");
    gets(str);

    while (str[count] != '
#include <stdio.h>

main(){
    char str[30];
    int i, j, count = 0, aux = 0, c = 1;

    puts("Informe a String");
    gets(str);

    while (str[count] != '%pre%'){
        count++;
    }

    for(i = 0; i < count; i++){ 
        aux = 1; 
        for(j = i + 1; j < count; j++){ 
            if(str[i] == str[j]){ 
                aux++; 
            } else
                break;
        }   
        if(i == 0) 
          c = 1; 
        else{
          for(j = i - 1; j >= 0; j--){ 
            if(str[i] != str[j])
              c = 1;
            else{ 
              c = 0;
              break;
            }
          }
        }
        if(c == 1)
        printf("\n%c : %d", str[i], cont);
    }


}
'){ count++; } for(i = 0; i < count; i++){ aux = 1; for(j = i + 1; j < count; j++){ if(str[i] == str[j]){ aux++; } else break; } if(i == 0) c = 1; else{ for(j = i - 1; j >= 0; j--){ if(str[i] != str[j]) c = 1; else{ c = 0; break; } } } if(c == 1) printf("\n%c : %d", str[i], cont); } }
    
26.03.2018 / 07:04
0

I could not put it as a comment, but a general solution is you sort your string and then go by counting how many times each character occurs (how many times in a row).

    
26.03.2018 / 16:23
0

Here is an example of how to count occurrences of the same character present in a string in C . (This example does not consider chars 'A' and 'a' equal).

#include "stdio.h"
#include "stdlib.h"

int isAlpha(char c) {
  if(c >= 48 && c &lt= 57) { // [0-9]
    return 1;
  }

  if(c >= 65 && c &lt= 90) { // [A-Z]
    return 1; 
  }

  if(c >= 97 && c &lt= 122) { // [a-z]
    return 1;
  }

  return 0;
}

int main(void) {
    int *map = (int *)calloc(128, sizeof(int));
    int i = 0;

    char *test = "banana";
    char tmp;

    do {
        tmp = test[i++];
        if (isAlpha(tmp)) {
            map[tmp]++;
        }
    } while (tmp != '
#include "stdio.h"
#include "stdlib.h"

int isAlpha(char c) {
  if(c >= 48 && c &lt= 57) { // [0-9]
    return 1;
  }

  if(c >= 65 && c &lt= 90) { // [A-Z]
    return 1; 
  }

  if(c >= 97 && c &lt= 122) { // [a-z]
    return 1;
  }

  return 0;
}

int main(void) {
    int *map = (int *)calloc(128, sizeof(int));
    int i = 0;

    char *test = "banana";
    char tmp;

    do {
        tmp = test[i++];
        if (isAlpha(tmp)) {
            map[tmp]++;
        }
    } while (tmp != '%pre%');

    for (i = 0; i &lt 128; i++) {
        if (map[i] != 0) {
            printf("%c = %d\n", i, map[i]);
        }
    }

    return 0;
}
'); for (i = 0; i &lt 128; i++) { if (map[i] != 0) { printf("%c = %d\n", i, map[i]); } } return 0; }
    
26.03.2018 / 19:30