Thread: Help Needed please! (Basic)

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Unhappy Help Needed please! (Basic)

    I am trying to get the programme to firstly print the ascii values of the first ten chars in the array s. I am also trying to build a function that counts the number of 4 letter words typed into it.

    Any help would be hugely appreciated, thanks!
    Last edited by Acer; 10-05-2010 at 12:42 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You've got a start.

    You could tokenize your string with strtok() and check the length of each token. If the length is 4 characters, increment a counter.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by Acer View Post
    I am trying to get the programme to firstly print the ascii values of the first ten chars in the array s. I am also trying to build a function that counts the number of 4 letter words typed into it.

    Any help would be hugely appreciated, thanks!

    Code:
    #include <stdio.h>
    
    main()
    {
    void show( const char s[]);
    
    int mylen( const char * s);
    
    char buff[100];
    while (scanf(" %s", buff) != EOF){
       // Hold a zero terminated string
        }
    }
    Counting can be done like this:
    Code:
    char* buffer = "Hello World";
    int i;
    for (i = 0 ; i < 10 && i < strlen(buffer) ; ++i)
    {
       printf("Letter %c in ASCII is %d\n", buffer[i], buffer[i]);
    }
    A integer and a character are basically the same thing, except since a character is simply 1 byte (typically), it can only count up to 255, while an integer is 4 bytes (typically). It's all how you choose to display the information (notice the variable formatting in the printf() function, the first one specifies that it be print out as a char, as one would expect, but the %d specifies that it be printed out as a decimal, which is its ASCII value.

    As for counting the number of 4 letter words, use a string tokenizer, a fairly common tool for parsing strings.
    Code:
    int count = 0;
    char* buffer = "These are some words, not all of them are four letters"
    char* token = strtok(buffer, " \t");
    while (token)
    {
       if (strlen(token) == 4)
          count++;
       token = strtok(NULL, " \t");
    }
    A tokenizer takes a string (char buffer), and a string of characters representing delimiters (separators). Subsequent calls to the tokenizer take a NULL pointer as the first parameter which tells it to continue where it left off last time. The tokenizer will return a NULL pointer when there are no more tokens left.

    A token is just a "word" so to speak - that is, anything in between the delimiters, in the case above, I set the delimiters to spaces and tabs (general whitespace) with the string " \t".

    Hope that helps ya out some.

    God I hope that's not homework...

    Feel free to PM me if you have any more specific questions, sometimes I don't see responses to my posts.
    Last edited by Syndacate; 10-05-2010 at 12:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists Basic Help Needed
    By pobri19 in forum C++ Programming
    Replies: 5
    Last Post: 08-24-2008, 04:57 AM
  2. Basic C-- Help Desperately Needed
    By toadkiwi in forum C Programming
    Replies: 10
    Last Post: 02-14-2008, 11:08 PM
  3. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM
  4. VB vs. BASIC
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 07-05-2002, 08:55 PM
  5. DJGPP help needed
    By dune911 in forum C++ Programming
    Replies: 6
    Last Post: 09-15-2001, 04:56 PM