C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 10th, 2002, 09:20 PM
cynsim cynsim is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 0 cynsim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to cynsim
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...

Reply With Quote
  #2  
Old November 10th, 2002, 09:55 PM
Rdesign Rdesign is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Redding
Posts: 49 Rdesign User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #3  
Old November 10th, 2002, 09:58 PM
cynsim cynsim is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 0 cynsim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to cynsim
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.

Reply With Quote
  #4  
Old November 10th, 2002, 10:59 PM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 594 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 6 m 15 sec
Reputation Power: 21
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

Reply With Quote
  #5  
Old November 11th, 2002, 08:47 AM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
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/

Reply With Quote
  #6  
Old November 12th, 2002, 02:01 PM
Rdesign Rdesign is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Redding
Posts: 49 Rdesign User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > c prog - initializing arrays question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway