how to implement this ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madeofdata
    New Member
    • Dec 2008
    • 13

    how to implement this ?

    what is the code to implement this function?
    countCharInText
    Counts the frequency of a character in a text
    the prototype is
    int countCharInText (char* text, int size, char toCount);
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Loop through alll the characters in text and check whether each one is equal to toCount. We can't give you the code for it because you're supposed to do the assignment.
    Hope this helps.

    Comment

    • madeofdata
      New Member
      • Dec 2008
      • 13

      #3
      this not an assignement I am looking for the code because I need to review for my test and I am not able to impelment the function..so please if you can help me a bit more....

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        Okay, here's pseudocode:
        Code:
        Set a counter variable to zero.
        For each character in text:
            If it's equal to toCount:
                Increment the counter variable.
        Return the counter variable.
        I hope this is more helpful. :-)

        Comment

        • madeofdata
          New Member
          • Dec 2008
          • 13

          #5
          but there is a problem , for the space and new lignes what should I do?

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Originally posted by madeofdata
            but there is a problem , for the space and new lignes what should I do?
            What makes you think there's anything special about space and new line characters? Either the next character in the string matches or it doesn't.

            I suggest you post a source code snippet that steps through each character in the string (the 'for' loop in boxfish's pseudocode). No need to do anything in the loop right now. Based on your questions in this thread and in your myStrCmp thread I think you might be having trouble with this task.

            Comment

            • madeofdata
              New Member
              • Dec 2008
              • 13

              #7
              this is my function :
              int countCharInText (char* text, int size, char toCoun){
              char c = 0;
              int i = 0;
              int frequence = 0;

              for(i = 0; i < size-1; i++)
              {
              if(text[i] == toCount)
              frequence++;
              }
              return frequence;
              }

              is it correct ?
              + how the main function should be knowing that I cannot use a scanf and that the sentinel is a "."(a dot)

              Comment

              • boxfish
                Recognized Expert Contributor
                • Mar 2008
                • 469

                #8
                Originally posted by madeofdata
                this is my function :
                int countCharInText (char* text, int size, char toCoun){
                char c = 0;
                int i = 0;
                int frequence = 0;

                for(i = 0; i < size-1; i++)
                {
                if(text[i] == toCount)
                frequence++;
                }
                return frequence;
                }

                is it correct ?
                + how the main function should be knowing that I cannot use a scanf and that the sentinel is a "."(a dot)
                It would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks. Anyway, yes, that function looks right, except for that you don't need to declare c, and the condition for your for loop seems to skip the last letter. I'm not sure about the main function; maybe someone else who knows more about c i/o can help with that.
                Good luck on your exam.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Originally posted by madeofdata
                  this is my function :
                  Code:
                  int countCharInText(char* text, int size, char toCoun){
                  char c = 0;  
                       int i = 0;
                       int frequence = 0;
                  
                       for(i = 0; i < size-1; i++)
                       {
                            if(text[i] == toCount)
                                 frequence++;
                       }
                       return frequence;
                  }
                  is it correct ?
                  If your input text is a null-terminated string then you don't need to pass in the size -- keep comparing until you find the null character ('\0').

                  Comment

                  Working...