string functions

This is a discussion on string functions within the C Programming forums, part of the General Programming Boards category; Code: #include <string.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAX 100 void getData (char **words, char** copyWords,int repeat[], int ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    2

    string functions(edited) need assitance

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    #define MAX 100
    
    
    void getData (char **words, char** copyWords,int repeat[], int *num);
    void clean   (char word[]);
    void sortSelect(char** arr, int num);
    
    
    int main(void)
    {
        char* words[MAX]; int repeat[MAX] = {0};  int num;
        char* copyWords[MAX]; int i;
    
    
    
    
        getData(words, copyWords, repeat, &num);
        sortSelect(words,num);
    
    
        return 0;
    }
    
    
    void getData (char **words, char** copyWords,int repeat[], int *num)
    {    
        int i = 0; int j = 0; int count = 0;
        FILE* fpIn;
        char temp[25];
        int len; 
    
    
        if ((fpIn = fopen ("data.txt","r"))==NULL)
           {
               printf ("ERROR OPENING FILE");
               exit (100);
               //prints error message if the file cannot be accessed
           }
    
    
        while(fscanf(fpIn,"%s",temp)!=EOF)
        {
             clean(temp);
             len = strlen(temp) + 1;
             words[i] = (char*) calloc (len, sizeof (char));
             copyWords[i] = (char*) calloc (len, sizeof (char));
             strncpy(words[i], temp, len);    
             strncpy(copyWords[i],temp, len);
             i++;
        }
        *num = i;
        return;
    }
    
    
    void clean   (char word[])
    {  
        int i; int j; int num;
    
    
        num = strlen(word);
        
        for(i=0;i<num;i++)
        {
            if (ispunct(word[i]) > 0)
            {
                if (i == 0)
                {
                    for(j=0;j<num;j++)
                        word[j] = word[j+1];
                }//if
                else
                  word[i] = '\0';
            }//if
            word[i] = toupper(word[i]);
        }//for
    
    
        return;
    }
    
    
    void sortSelect(char** arr, int num)
    {
        int current; int walker;
        int smallestIndex;
        char* temp;
        
        for (current = 0; current < num - 1; current++)
        {
            smallestIndex = current;
            for (walker = current; walker < num; walker ++)
              {
                    if (strcmp(arr[walker], arr[smallestIndex])<0)
                      smallestIndex = walker;
              }//for walker
            
            //Swap to position smallest at what is the current position
            temp = arr[current];
            arr[current] = arr[smallestIndex]; 
            arr[smallestIndex] = temp;  
    
    
        }//for current 
      return;
    }
    I'm having trouble with the string functions.

    I'm suppose to store the input in the array of strings and then the clean function capitalizes all the letters and takes out any punctuation marks. After I have done that, I need to count how many times the words repeat. That's why I made two array of strings so that I could only store one of each words and count the times it has been repeated.

    int repeat[] was made in order for me to count the times the word has been repeated.

    The problem is I don't understand how to store in a different array of strings when a new word comes up. Also how do I count the times the word has been repeated?

    I know I'm suppose to allocate the memory each time a new word comes up(for the copyWord[]), but I just duplicated the original cleaned array of strings by accident. I tried using strncmp function to compare the two arrays, but I was unsuccessful.

    I hope that all made sense...
    Last edited by wonboy7248; 06-05-2012 at 09:33 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Perhaps write a function like this

    void findWord(char** arr, int num, char *word);
    This searches the array you have for word.

    If it's found, then you increment the count, otherwise you
    - add the word (it's new)
    - set the count to 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about functions of string vs char string
    By Robertjh12 in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2011, 03:13 AM
  2. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  3. Reverse a string (without using any string functions?)
    By geetard in forum C Programming
    Replies: 2
    Last Post: 11-15-2006, 06:42 PM
  4. String functions help
    By kippwinger in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2003, 04:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21