![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 19
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4
![]() |
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] |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jul 2005
Posts: 19
Rep Power: 0
![]() |
Hi L7Sqr,
would you show me some examples for that..thanks for your quick reply |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jul 2005
Posts: 19
Rep Power: 0
![]() |
i have to program it in C language..Thanks
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jul 2005
Posts: 19
Rep Power: 0
![]() |
Anyone has a sample code with my case..please
|
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
can you post any code where you defined the "string" so we can help you better.
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Jul 2005
Posts: 19
Rep Power: 0
![]() |
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; } |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Jul 2005
Posts: 19
Rep Power: 0
![]() |
any idea?
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Jul 2005
Posts: 19
Rep Power: 0
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|