#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Question Need some help, it's easy


    Hey guys, i need some help, it might be simple for those who have some experience...

    I'm writing a program that reads the sentence and show how many times each letter appeared, exemple:

    Input:
    Today I arrived earlier from my job.

    Output:
    A:***(3)
    B:*(1)
    C:(0)
    D:**(2)
    E:**(2)
    F:*(1)
    G:(0)
    H:(0)
    I:***(3)
    ...


    OK, I did it already, it's down there \/

    now I need to make the process stop when it reads a line that start with the word FIM (means END in portuguese). I was thinking about using strstr

    help!

    #EDITED
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    26
    Rep Power
    0
    What Do You Need Help With I Can't Read Your Mind
    Post What You Have Done.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    If you had an integer array of length 26,
    int frequency[26];

    and if you initialized all the entries to zero,
    memset(frequency,more, arguments);

    then you read the sentence one character at a time
    some sort of loop {
    char c = fgetc(FILE_pointer_source);
    check for sentence termination, including end of file;

    You could convert that character to upper case
    #include <ctype.h>
    toupper

    And if that character were a letter
    #include <ctype.h>
    isalpha

    and here's the tricky part
    you could subtract 'A' from c
    and use that number as an index into frequency
    ++frequency[c-'A']

    } /* end of some sort of loop */


    Writing the characters, asterisks, and numbers shouldn't trouble you.



    [edit]ooh. On second thought, the ASCII character set is only 7 bits, or 8 bits, depending, but either way, small. You might consider counting everything and limiting your report . You'll still need to detect end of sentence and combine lower with upper case in the report. That's easy though.[/edit]
    Last edited by b49P23TIvg; May 13th, 2013 at 09:46 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Ah, what the heck.

    Code:
    #include<stdio.h>
    #include<string.h> //for strlen()
    
    int main()
    {
    	int iterate;
    	int length;
    	unsigned int ASCII;
    	char sentence[] = "APPLES ARE AWESOME";
    	int alphabet[26];
    	for(iterate = 0;iterate < 26;iterate++)
    	{
    		alphabet[iterate] = 0;
    	} //Arrange ASCII nums
    	length = strlen(sentence);
    	for(iterate = 0;iterate < length;iterate++)
    	{
    		ASCII = sentence[iterate];
    		if(ASCII >= 65 && ASCII <= (65 + 26))
    		{
    			alphabet[ASCII-65]++;
    		}
    	}
    	for(iterate = 65;iterate < (65 + 26);iterate++)
    	{
    		printf("The letter %c is used %d\n",iterate,alphabet[iterate - 65]);
    	}
    	getchar();
    	return 0;
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    9
    Rep Power
    0
    lol... I was stupid for asking for help before trying... I did it without any help and it was pretty easy... here it is:

    #include <stdio.h>
    #include <string.h>

    int main(){
    int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,v=0,w=0,x=0,y=0, z=0,zero=0,um=0,dois=0,tres=0,quatro=0,cinco=0,seis=0,sete=0,oito=0,nove=0,num=0,ast;
    char texto[999999], fim[3]={'F','I','M'};

    fgets(texto, 1001, stdin);

    for(num=0; num<strlen(texto); num++){
    switch(texto[num]){
    case 'a':
    a = a +1;
    break;
    case 'b':
    b= b+1;
    break;
    case 'c':
    c = c+1;
    break;
    case 'd':
    d = d+1;
    break;
    case 'e':
    e++;
    break;
    case 'f':
    f++;
    break;
    case 'g':
    g++;
    break;
    case 'h':
    h++;
    break;
    case 'i':
    i++;
    break;
    case 'j':
    j++;
    break;
    case 'k':
    k++;
    break;
    case 'l':
    l++;
    break;
    case 'm':
    m++;
    break;
    case 'n':
    n++;
    break;
    case 'o':
    o++;
    break;
    case 'p':
    p++;
    break;
    case 'q':
    q++;
    break;
    case 'r':
    r++;
    break;
    case 's':
    s++;
    break;
    case 't':
    t++;
    break;
    case 'u':
    u++;
    break;
    case 'v':
    v++;
    break;
    case 'w':
    w++;
    break;
    case 'x':
    x++;
    break;
    case 'y':
    y++;
    break;
    case 'z':
    z++;
    break;
    case '0':
    zero++;
    break;
    case '1':
    um++;
    break;
    case '2':
    dois++;
    break;
    case '3':
    tres++;
    break;
    case '4':
    quatro++;
    break;
    case '5':
    cinco++;
    break;
    case '6':
    seis++;
    break;
    case '7':
    sete++;
    break;
    case '8':
    oito++;
    break;
    case '9':
    nove++;
    break;
    }
    }
    printf("Caracteres:\n");
    printf("a: ");
    for (ast=0;ast<a;ast++){
    printf("*");
    }
    printf (" (%d)", a);
    printf("\n");
    printf("b: ");
    for (ast=0;ast<b;ast++){
    printf("*");
    }
    printf (" (%d)", b);
    printf("\n");
    printf("c: ");
    for (ast=0;ast<c;ast++){
    printf("*");
    }
    printf (" (%d)", c);
    printf("\n");
    printf("d: ");
    for (ast=0;ast<d;ast++){
    printf("*");
    }
    printf (" (%d)", d);
    printf("\n");
    printf("e: ");
    for (ast=0;ast<e;ast++){
    printf("*");
    }
    printf (" (%d)", e);
    printf("\n");
    printf("f: ");
    for (ast=0;ast<f;ast++){
    printf("*");
    }
    printf (" (%d)", f);
    printf("\n");
    printf("g: ");
    for (ast=0;ast<g;ast++){
    printf("*");
    }
    printf (" (%d)", g);
    printf("\n");
    printf("h: ");
    for (ast=0;ast<h;ast++){
    printf("*");
    }
    printf (" (%d)", h);
    printf("\n");
    printf("i: ");
    for (ast=0;ast<i;ast++){
    printf("*");
    }
    printf (" (%d)", i);
    printf("\n");
    printf("j: ");
    for (ast=0;ast<j;ast++){
    printf("*");
    }
    printf (" (%d)", j);
    printf("\n");
    printf("k: ");
    for (ast=0;ast<k;ast++){
    printf("*");
    }
    printf (" (%d)", k);
    printf("\n");
    printf("l: ");
    for (ast=0;ast<l;ast++){
    printf("*");
    }
    printf (" (%d)", l);
    printf("\n");
    printf("m: ");
    for (ast=0;ast<m;ast++){
    printf("*");
    }
    printf (" (%d)", m);
    printf("\n");
    printf("n: ");
    for (ast=0;ast<n;ast++){
    printf("*");
    }
    printf (" (%d)", n);
    printf("\n");
    printf("o: ");
    for (ast=0;ast<o;ast++){
    printf("*");
    }
    printf (" (%d)", o);
    printf("\n");
    printf("p: ");
    for (ast=0;ast<p;ast++){
    printf("*");
    }
    printf (" (%d)", p);
    printf("\n");
    printf("q: ");
    for (ast=0;ast<q;ast++){
    printf("*");
    }
    printf (" (%d)", q);
    printf("\n");
    printf("r: ");
    for (ast=0;ast<r;ast++){
    printf("*");
    }
    printf (" (%d)", r);
    printf("\n");
    printf("s: ");
    for (ast=0;ast<s;ast++){
    printf("*");
    }
    printf (" (%d)", s);
    printf("\n");
    printf("t: ");
    for (ast=0;ast<t;ast++){
    printf("*");
    }
    printf (" (%d)", t);
    printf("\n");
    printf("u: ");
    for (ast=0;ast<u;ast++){
    printf("*");
    }
    printf (" (%d)", u);
    printf("\n");
    printf("v: ");
    for (ast=0;ast<v;ast++){
    printf("*");
    }
    printf (" (%d)", v);
    printf("\n");
    printf("w: ");
    for (ast=0;ast<w;ast++){
    printf("*");
    }
    printf (" (%d)", w);
    printf("\n");
    printf("x: ");
    for (ast=0;ast<x;ast++){
    printf("*");
    }
    printf (" (%d)", x);
    printf("\n");
    printf("y: ");
    for (ast=0;ast<y;ast++){
    printf("*");
    }
    printf (" (%d)", y);
    printf("\n");
    printf("z: ");
    for (ast=0;ast<z;ast++){
    printf("*");
    }
    printf (" (%d)", z);
    printf("\n");
    printf("\n");
    printf("Digitos:\n");
    printf("0: ");
    for (ast=0;ast<zero;ast++){
    printf("*");
    }
    printf (" (%d)", zero);
    printf("\n");
    printf("1: ");
    for (ast=0;ast<um;ast++){
    printf("*");
    }
    printf (" (%d)", um);
    printf("\n");
    printf("2: ");
    for (ast=0;ast<dois;ast++){
    printf("*");
    }
    printf (" (%d)", dois);
    printf("\n");
    printf("3: ");
    for (ast=0;ast<tres;ast++){
    printf("*");
    }
    printf (" (%d)", tres);
    printf("\n");
    printf("4: ");
    for (ast=0;ast<quatro;ast++){
    printf("*");
    }
    printf (" (%d)", quatro);
    printf("\n");
    printf("5: ");
    for (ast=0;ast<cinco;ast++){
    printf("*");
    }
    printf (" (%d)", cinco);
    printf("\n");
    printf("6: ");
    for (ast=0;ast<seis;ast++){
    printf("*");
    }
    printf (" (%d)", seis);
    printf("\n");
    printf("7: ");
    for (ast=0;ast<sete;ast++){
    printf("*");
    }
    printf (" (%d)", sete);
    printf("\n");
    printf("8: ");
    for (ast=0;ast<oito;ast++){
    printf("*");
    }
    printf (" (%d)", oito);
    printf("\n");
    printf("9: ");
    for (ast=0;ast<nove;ast++){
    printf("*");
    }
    printf (" (%d)", nove);
    printf("\n");

    system("pause");

    return 0;
    }





    OK, now i need to make the process stop when it read a line that starts with the word "FIM" (means END in portuguese). I'm thiking about using strstr...
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by b49P23TIvg
    If you had an integer array of length 26,
    int frequency[26];

    and if you initialized all the entries to zero,
    memset(frequency,more, arguments);

    then you read the sentence one character at a time
    some sort of loop {
    char c = fgetc(FILE_pointer_source);
    check for sentence termination, including end of file;

    You could convert that character to upper case
    #include <ctype.h>
    toupper

    And if that character were a letter
    #include <ctype.h>
    isalpha

    and here's the tricky part
    you could subtract 'A' from c
    and use that number as an index into frequency
    ++frequency[c-'A']

    } /* end of some sort of loop */


    Writing the characters, asterisks, and numbers shouldn't trouble you.



    [edit]ooh. On second thought, the ASCII character set is only 7 bits, or 8 bits, depending, but either way, small. You might consider counting everything and limiting your report . You'll still need to detect end of sentence and combine lower with upper case in the report. That's easy though.[/edit]
    Hehe, I'm just 18, it's my first year in university, and it's the first program language that I'm learning... what you said is too difficult for me =P, I did using other commands, but TY anyway... if you can help me with my other problem ;)...

IMN logo majestic logo threadwatch logo seochat tools logo