Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 1st, 2005, 10:01 PM   #1
Bernard
Newbie
 
Join Date: Jul 2005
Posts: 19
Rep Power: 0 Bernard is on a distinguished road
inserting symbol into a string

how can i insert a single char(could be any symbol) into a string.For example, Variable A stores the value of "D:\program files\common\test". Now, i would like to add a symbol "\" everytime it finds the symbol"\". The output i wanna get is "D:\\program files\\common\\test". Does anyone know how to do it..Thanks for your help..

Best Regards,
Bernard
Bernard is offline   Reply With Quote
Old Aug 1st, 2005, 10:07 PM   #2
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4 L7Sqr is on a distinguished road
A 'string' is simply an array of characters.
Find the length of the string.
Allocate a guestimate of 1.5 times that size for a new array.
Start copying from the beginning of the first into the second, and every time you get to a '\' enter an extra in the second array.
Check before each character that you have not exceeded your 1.5 guestimate size.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame."
- The Quiet Things That No One Ever Knows [BrandNew]
L7Sqr is offline   Reply With Quote
Old Aug 1st, 2005, 10:10 PM   #3
Bernard
Newbie
 
Join Date: Jul 2005
Posts: 19
Rep Power: 0 Bernard is on a distinguished road
Hi L7Sqr,
would you show me some examples for that..thanks for your quick reply
Bernard is offline   Reply With Quote
Old Aug 1st, 2005, 10:12 PM   #4
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
Does it have to be done in C or is it permissible to do in C++?

ps: if you have some code already started, that would help others help you.
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Old Aug 1st, 2005, 10:21 PM   #5
Bernard
Newbie
 
Join Date: Jul 2005
Posts: 19
Rep Power: 0 Bernard is on a distinguished road
i have to program it in C language..Thanks
Bernard is offline   Reply With Quote
Old Aug 1st, 2005, 10:37 PM   #6
Bernard
Newbie
 
Join Date: Jul 2005
Posts: 19
Rep Power: 0 Bernard is on a distinguished road
Anyone has a sample code with my case..please
Bernard is offline   Reply With Quote
Old Aug 1st, 2005, 10:42 PM   #7
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
can you post any code where you defined the "string" so we can help you better.
OpenLoop is offline   Reply With Quote
Old Aug 1st, 2005, 10:53 PM   #8
Bernard
Newbie
 
Join Date: Jul 2005
Posts: 19
Rep Power: 0 Bernard is on a distinguished road
int main(int argc, char *argv[])
{
strcpy(writetofile, argv[3]);
strncpy (DirSpec, argv[1], strlen(argv[1])+1);
strcpy(filename, "\\");
strcat(filename, argv[2]);
strncat (DirSpec, filename, 18);
hFind = FindFirstFile(DirSpec, &FindFileData);
fout = fopen(writetofile, "w");
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid file handle. Error is %u\n", GetLastError());
return (-1);
}
else
{
printf ("%s\n", &FindFileData.cFileName);
fprintf(fout, "%s \n", &FindFileData.cFileName);
while (FindNextFile(hFind, &FindFileData) != 0)
{
printf ("%s\n", &FindFileData.cFileName);
fprintf(fout, "%s \n", &FindFileData.cFileName);
}

dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
printf ("Error of finding files. Error is %u\n", dwError);
return (-1);
}
}
fclose(fout);
fout = fopen(writetofile, "r");
while (fgets(record, MAX_REC_SIZE + 1, fout) != NULL)
{
strcpy(fileaddress, argv[1]);
strcat(fileaddress, "\\");
fputs(record, stdout);
printf("record is %s\n", record);
strcat(fileaddress, record);
printf("fileaddress is %s \n", fileaddress);
if (!stat(fileaddress , &buf))
{
strftime(timeStr, 100, "%d-%m-%Y %H:%M:%S", localtime( &buf.st_mtime));
printf("\nLast modified date and time = %s\n", timeStr);
}
else
{
printf("error getting atime or file does not exists.\n");
}
time(&now);
time_now = localtime(&now);
c = asctime(time_now);
set_day(&now);
month_add(&now, -24);
time_new = mktime(time_now);
tm_time_new = localtime(&time_new);
d = asctime(tm_time_new);
puts(d);
timediff = difftime(time_new ,buf.st_mtime);
printf("\nDiff between current and last modified time ( in seconds ) %f\n", timediff );
if (timediff < 0)
{
//remove(filename);
printf("file %s has been removed \n", filename);
}
else
{
printf("file not yet expired \n");
}
//fclose(fout);
}
/*
if (!stat(filename, &buf))
{
strftime(timeStr, 100, "%d-%m-%Y %H:%M:%S", localtime( &buf.st_mtime));
printf("\nLast modified date and time = %s\n", timeStr);
}
else
{
printf("error getting atime or file does not exists.\n");
}
time(&now);
time_now = localtime(&now);
c = asctime(time_now);
set_day(&now);
month_add(&now, -24);
//time_now->tm_mday = 1;
time_new = mktime(time_now);

tm_time_new = localtime(&time_new);
d = asctime(tm_time_new);
puts(d);
timediff = difftime(time_new ,buf.st_mtime);
printf("\nDiff between current and last modified time ( in seconds ) %f\n", timediff );
if (timediff < 0)
{
//remove(filename);
printf("file %s has been removed \n", filename);
}
else
{
printf("file not yet expired \n");
}
*/
fclose(fout);
return 0;
}
Bernard is offline   Reply With Quote
Old Aug 1st, 2005, 11:09 PM   #9
Bernard
Newbie
 
Join Date: Jul 2005
Posts: 19
Rep Power: 0 Bernard is on a distinguished road
any idea?
Bernard is offline   Reply With Quote
Old Aug 1st, 2005, 11:18 PM   #10
Bernard
Newbie
 
Join Date: Jul 2005
Posts: 19
Rep Power: 0 Bernard is on a distinguished road
this function is still missing something...i've been trying very hard but still couldnt come out with the correct output..please help me with that
my input would be
statement = d:\program files\common\internet
insert char = "\\"
i want my output to be like this d:\\program files\\common\\internet

void addchar(char* statement, char* insertchar)
{
char *temp = statement;
while(*statement !=0)
{
if (*statement == *insertchar)
{
*temp = *statement;
*temp++;
}
*statement++;
}
}
please help
Bernard is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC