#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    0
    Rep Power
    0

    c prog - initializing arrays question


    I'm working on a problem that needs me to compare two strings together.

    The problem is this, I'd like to be able to split up one of the strings so that I can put one word per array.

    For example if the first string reads as such:

    The brown cow ate grass.

    I'd like to have an array for each of the words

    word [0] = "The"
    word [1] = "brown"

    etc...

    so that I can compare it to another string.
    The data for both strings comes from a text file. Can anyone give me an idea on how to do this? Or maybe a better way to compare the two strings together?

    Thanks ahead of time...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    Redding
    Posts
    49
    Rep Power
    13
    One word per array is no problem. One word per array Element is a different story. You might already know this but in C, a string is an array of char variables. So if you want to compare each element of a string you might try:

    # include <iostream>
    # include <cstring>
    using namespace std;
    int main() {
    char st1[80] = "The brown cow chewed grass";
    char st2[80] = "The brown cow ate grass";

    for (int a=0; a<strlen(st1); a++) {
    if (st1[a] != st2[a]) {
    cout << "The strings are different!" << '\n';
    return 0;
    } else {
    cout << "The strings are the same!" << '\n';
    return 0;
    }
    }

    I think that's right...at least close :-)

    I hope this helps.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    0
    Rep Power
    0
    yah I'm not sure how to get it so it will get just a word into the first array, then stop at the whitespace, increment the index, read the next word, put it in the array, increment again.
  6. #4
  7. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    I would use a pointer to the string and to the array element but simply


    isspace() to find the spaces


    Code:
    for(i=0;i<lstrlen(sSentance);i++)
    {
             if(!isspace(sSentance[i]))
                      //add to the array element lstrcat()
                    
              if(isspace(sSentance[i]))
                      //alloc new word array element
                      //increment word array index
               
    }
    this is simpler using pointers
    Code:
    //this removes some punctuation from a string (to save the file name)
    	char				sTempBuffer[L_STRING],sNewString[L_STRING],*pNew=NULL, *pOld=NULL;
    	int					iLen=0;
    
    	iLen=lstrlen(sBuffer);
    	if(iLen>=L_STRING)
    		return FALSE;//not enough space
    	
    	sprintf(sTempBuffer,"%s",sBuffer);
    	pOld=sTempBuffer;
    	pNew=sNewString;
    
    	while(*pOld != '\0')
    	{
    		while(ispunct(*pOld))	*pOld++;
    		while(isspace(*pOld))	*pOld++;
    		*pNew++=*pOld++;
    	}
    	*pNew='\0';
    	sprintf(sBuffer,"%s",sNewString);
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    If your stdc lib has the strsep function (which it probably won't unless you're running the latest GNU libc), that function is a miracle worker for breaking up a sentence on a common delimiter.

    Barring that, I'd use the following C code:

    Code:
    char** word_list(char* sentence) {
      char** word;
      int wordcount = 0;
      char* pos = sentence;
    
      while(pos && *pos) {
        pos++;
        pos = strchr(pos, ' ');
        wordcount++;
      }
    
      word = (char**)calloc(wordcount + 1, sizeof(char*));
      
      wordcount = 0;
      pos = strtok(sentence, " ");
      while (pos) {
        word[wordcount++] = pos;
        pos = strtok(NULL, " ");
      }
    
      return word;
    
    }
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    Redding
    Posts
    49
    Rep Power
    13

    New plan...


    What do you think of using a multidimensional array?

    Ex: // straray.cpp
    // array of strings
    #include <iostream>
    using namespace std;

    int main()
    {
    const int DAYS = 7; //number of strings in array
    const int MAX = 10; //maximum size of each string
    //array of strings
    char star[DAYS][MAX] = { "Sunday", "Monday", "Tuesday",
    "Wednesday", "Thursday",
    "Friday", "Saturday" };
    for(int j=0; j<DAYS; j++) //display every string
    cout << star[j] << endl;
    return 0;

    }
    Does that give you any ideas?

IMN logo majestic logo threadwatch logo seochat tools logo