Thread: ascii codes!!

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    Post ascii codes!!

    i have an assigment that searches a string and find the numbers of each letters written in the string and add 1 to array[26]
    for instance stribg is aaaabbbb the code should add array[0] 4 and array[1] 4 i think i have to use ascii codes but i dont know how to do it?

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    Try this out...

    int main()
    {
    static int array[26];
    char *str;
    int i;
    printf("\nEnter The String: ");
    gets(str);
    while(*str!='\0')
    {
    if(*str>=65 && *str<=90)
    array[*str-65]+=1;
    else
    if(*str >=97 && *<123)
    array[*str-97]+=1;
    str++;
    }
    clrscr();
    printf("The following is the number of times each character has occured: \n);
    for(i=0;i<26;i++)
    {
    if(array[i]>0)
    printf("\n%c ==> %d Time(s)",i+65,array[i]);
    }
    return(0);
    }

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Hi vsriharsha,

    Nice try but your code is not save:

    Try to use fgets in stead of gets. This is more save because you can specify the lenght of your input buffer. Second, when you use gets (or fgets) you need to allocate space for your input buffer.

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h>
    #include <ctype.h>
    
    int main()
    {
        /* ASCII: A-Z = 65-90, a-z = 97-122 */
        char line[1024];
        char *ptr;
        int array[26];
        int i;
    
        memset(array, 0, 26 * sizeof(int));
    
        printf("Enter string\n");
        fgets(line, 1024, stdin);
    
        for(ptr = line; *ptr; ptr++)
            if(isalpha(*ptr))
                array[tolower(*ptr)-97] += 1;
    
        for(i = 0; i < 26; i++)
            printf("%c -> %d time%s\n", i+97, array[i], array[i] == 1 ? "" : "s");
    
        return 0;
    }

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    Nice try but your code is not save:

    Try to use fgets in stead of gets. This is more save because you can specify the lenght of your input buffer. Second, when you use gets (or fgets) you need to allocate space for your input buffer.
    Is it?? Atleast I've never had any problems with gets(). Remember I'm using TURBOC (and any initializations that u insist on will be taken care of by my compiler). Secondly, I dont need to specify the length. Ur code suffers the drawback that it cannot hold more than 1024 characters, whereas the other way u can accommodate more.
    If u still feel gets is a problem, then u can use scanf() safely instead...

    scanf("%[^\0]s",str);

    Send me an example situation where my code does not work... Remember, Portability is not my intention in the above program.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You have no problems with gets but how do you know condorx is using the same compiler? I tried your code using VC++ and I got an Access Violation on gets. Second you forget to initialize your array with zeroes. It may work with TURBO C but not on all compilers. Some compilers do not automatically initialize variables when you declare them.

    An example situation? Any situation when I run it with VC++

  6. #6
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb

    Second you forget to initialize your array with zeroes
    Thats the reason I declared my array as STATIC (which automatically initializes the array to 0's.) and I also do not claim this code to be Portable and I have no wonder knowing that it does not compile using VC++, cos it is for DOS.
    :-)

    Warm Regards,
    Sriharsha.

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    So you ask someone to try something out but you don't know if it works on his/her system?

    Okay, thats fine by me. But personally I always try to use generic code that works for (almost) all compilers (exept if I know which compiler that someone is using).

    Cheers,
    Monster

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    ascii codes

    i am using turbo c 3 thanks for your answers
    but do we have to use pointer?
    what happens if i dont use pointer
    Last edited by condorx; 04-25-2002 at 06:49 AM.

  9. #9
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You mean like this?:

    Code:
    char line[1024];
    int i = 0;
    
    fgets(line, 1024, stdin);
    
    for(i = 0; i < strlen(line); i++)
    {
        if(isalpha(line[i]))
        {
        }
    }

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is it?? Atleast I've never had any problems with gets().
    You will eventually.

    >Ur code suffers the drawback that it cannot hold more than
    >1024 characters, whereas the other way u can accommodate more.
    Except for the fact that you are trying to assign to memory that you don't own. This is an error, and should seg fault when run.

    >If u still feel gets is a problem, then u can use scanf() safely instead...
    No, that's exactly the same thing. gets is unsafe because it doesn't check for array boundaries like fgets. Your scanf code doesn't check for boundaries either so not only does it suffer from the gets syndrome, it has the bugs of scanf too.

    >Send me an example situation where my code does not work...
    It doesn't compile on my system for several syntax errors, but the following causes a segmentation fault on my system once the errors were fixed:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() 
    { 
      static int array[26]; 
      char *str; 
      int i; 
      printf("\nEnter The String: "); 
      gets(str); /* The program breaks here */
      while(*str!='\0') 
      { 
        if(*str>=65 && *str<=90) 
          array[*str-65]+=1; 
        else 
          if(*str >=97 && *str<123) 
            array[*str-97]+=1; 
        str++; 
      } 
      printf("The following is the number of times each character has occured:\n"); 
      for(i=0;i<26;i++) 
      { 
        if(array[i]>0) 
          printf("\n%c ==> %d Time(s)",i+65,array[i]); 
      } 
      return(0); 
    }
    >Portability is not my intention in the above program
    Portability isn't the problem, the program is wrong no matter what platform it's run on.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Angry

    THE CODE WORKS PERFECTLY WELLLLLLLL.

    I tried the example...PRELUDE

    It said...

    D ==> 1 Time(s)
    E ==> 2 Time(s)
    L ==> 1 Time(s)
    P ==> 1 Time(s)
    R ==> 1 Time(s)
    U ==> 1 Time(s)

    Now, I consider this working.....

    It gave me only a few warnings stating that str has not been initialized. Thats it. Now, Im using TurboC 2 and running the program under DOS window of Windows 98 Second Edition. I have 256MB RAM and a P-III 500Mhz Processor. Now, I think, its high time I know what really is differing on our systems and what is causing the damn problems...

    Perhaps, it would be better to start a FAQ thread for knowing the limitations and Capabilities of different compilers...




    Cheers,
    Sriharsha.
    Help everyone you can

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >THE CODE WORKS PERFECTLY WELLLLLLLL.
    fflush ( stdin ); works on my system, but that doesn't make it right. void main also works, but we all know that it's wrong. Both are undefined and can cause serious problems, just because they work for you doesn't mean that they're correct.

    >its high time I know what really is differing on our systems and
    >what is causing the damn problems...
    Every system will be different, every compiler is different. This is why C has a standard, so that we can all program in a portable way without our programs breaking when they are used on another compiler or OS.

    -Prelude
    My best code is written with the delete key.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Portability is not my intention in the above program
    But what about the other portability problem - namely your 'portable' skill as a C programmer?

    Sure you may not care whether any single program is portable or not, but what should really concern you is whether all your learnt experiences will travel well to other compilers, operating systems, languages etc.

    Because one this is for sure, unlearning all these sloppy coding habits is going to be really hard work. Learning C is hard enough when you don't know anything - it's a lot more difficult if you've got a load of bad habits to get past.

  14. #14
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Red face

    just because they work for you doesn't mean that they're correct.

    Every system will be different, every compiler is different. This is why C has a standard, so that we can all program in a portable way without our programs breaking when they are used on another compiler or OS.
    Well...I'd take care of that Prelude. BTW, what compilers do people generally use? This is just for my information so that I too can get a finer grip on my subject. I generally check my programs with Turbo C and Borland C. And, can u suggest me a good site that hosts the ANSI coding standards for C.

    i am using turbo c 3 thanks for your answers
    but do we have to use pointer?
    what happens if i dont use pointer
    I also c that we have deviated from the originator of the thread's question. I feel its not mandatory to use pointers, you could get along well otherwise too...

    Regards,
    Sriharsha.
    Help everyone you can

  15. #15
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    I was a bit late to note the URL's u had displayed Prelude. They straighten out all my problematic assumptions. I should be able to refine all my coding habits once I go through all of em...
    Thnx. again for the site...

    Regards,
    Harsha.
    Help everyone you can

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about ASCII codes
    By rwmarsh in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2006, 10:49 AM
  2. ASCII Codes
    By X PaYnE X in forum Windows Programming
    Replies: 1
    Last Post: 12-31-2003, 01:37 PM
  3. Getting ASCII codes
    By moonwalker in forum C Programming
    Replies: 3
    Last Post: 07-24-2002, 01:17 PM
  4. ASCII Codes
    By drdroid in forum C++ Programming
    Replies: 11
    Last Post: 05-02-2002, 04:00 PM
  5. converting scan codes to ascii codes
    By stupid_mutt in forum C Programming
    Replies: 11
    Last Post: 01-25-2002, 04:06 PM