Computing.Net > Forums > Programming > Explode Array In C

Explode Array In C

Reply to Message Icon

Original Message
Name: Gravis
Date: September 18, 2002 at 09:14:29 Pacific
Subject: Explode Array In C
OS: Win2k Ser
CPU/Ram: P4
Comment:

How can I take only part of and array. If the array slot holds this:

words.doc

but I only want this:

words

How can I explode this or split it so I only get whats before the period. Thanks


Report Offensive Message For Removal


Response Number 1
Name: cup
Date: September 18, 2002 at 09:45:22 Pacific
Reply: (edit)

char bomb[] = "words.doc";

/* Find the dot */
char* dot = strchr(bomb,'.');

/* Terminate string at dot */
*dot = '\0';

printf (bomb); /* you should get words */


Report Offensive Follow Up For Removal

Response Number 2
Name: Sajid Mohammed
Date: September 18, 2002 at 10:09:14 Pacific
Reply: (edit)

Supposing str[] is the array in which you stored the word and substr[] is the array in which you are storing the substring.
i=0;
while(str[i]!='.')
{
substr[i]=str[i];
i=i+1;
}
substr[i]='\0';
_________________

Sajid Mohammed ( sajleo@yahoo.com )


Report Offensive Follow Up For Removal

Response Number 3
Name: Sajid Mohammed
Date: September 18, 2002 at 10:11:08 Pacific
Reply: (edit)

Also you can use strtok() function if you want to split the string into multiple substrings if there are more than one '.'

Sajid Mohammed ( sajleo@yahoo.com )


Report Offensive Follow Up For Removal

Response Number 4
Name: Gravis
Date: September 19, 2002 at 10:43:44 Pacific
Reply: (edit)

So when I add values to an array the array is not reset. For example if I put this in an array:

char person[20];

strcmp(person, "chuck");

Then I put:

person[] = '\0';

Will the first index in the array (person[0]) will that be a 'c' or a '\0'.

Thanks


Report Offensive Follow Up For Removal

Response Number 5
Name: Sajid Mohammed
Date: September 22, 2002 at 12:22:32 Pacific
Reply: (edit)

In your previous email you wrote
strcmp(person,"chuck") and I assume it to be strcpy(person,"chuck"). strcmp is to compare strings and strcpy is to copy string.
You cannot do person[]='\0'; as it is improper syntax. you have to give the index of the array when assigning a charecter.
'\0' is a character and person[] is not a character but person[0] or person[1] etc is a character.

A string is an array of characters.

Sajid Mohammed ( sajleo@yahoo.com )


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Explode Array In C

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge