C working with string problem

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 22
Reputation: Onixtender is an unknown quantity at this point 
Solved Threads: 0
Onixtender Onixtender is offline Offline
Newbie Poster

C working with string problem

 
0
  #1
Apr 23rd, 2008
Hi,
I have a problem creating one part of the program,
Lets say we have a line (from data file): 333 hou 23se 444 bi 4g
and the program should change to : house big
deleting first number if there is one ,and changing word parts to numbers, and deleting "just numbers" from line like 444...
code:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include<stdlib.h>
  4. #include <ctype.h>
  5. #define FILENAME "file.txt"
  6.  
  7. int main(void)
  8. {
  9. char record[100], /* array to hold lines */
  10. *fld1, *line,*kk; /* pointers to lines */
  11. FILE *fin; /* pointer to input file */
  12. int g,i; /* some integers... */
  13. fin = fopen(FILENAME, "r"); /* open the file to read */
  14.  
  15.  
  16. while (fgets(record, sizeof(record), fin))
  17. {
  18. if (record[strlen(record) - 1] != '\n')
  19. record[strlen(record) - 1] = '\n'; //until new line
  20. line = strtok(record, "\n"); //now we have a line to work with...
  21.  
  22. i = 0;
  23. g = strlen(line);
  24.  
  25. // this part of program should delete first number if there s one like : 33 dd to :dd
  26.  
  27. while( isdigit(line[i]))
  28. {
  29. line[i]= line[i+1] ; //somethings wrong... :(
  30. i=i+1;
  31. }
  32.  
  33.  
  34. // Ideas ?
  35.  
  36.  
  37.  
  38. printf("%s\n", line);
  39. }
  40. fclose(fin);
  41. system("Pause");
  42. return 0;
  43. }
Last edited by Ancient Dragon; Apr 23rd, 2008 at 11:03 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C working with string problem

 
0
  #2
Apr 23rd, 2008
lines 18 and 19. Why do you want to overwrite the last character read by '\n'? Don't you want to append it to the end of the string instead of overwriting the last character ? Actually I don't know why you want to do that anyway -- we normally just delete the '\n' if it exists.

line 20: useless line. record is already the full line that was read from the file. There is no reason to call strtok() for that.
Last edited by Ancient Dragon; Apr 23rd, 2008 at 11:16 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C working with string problem

 
0
  #3
Apr 23rd, 2008
>> ideas ?

Yes, here is one solution
  1. int main()
  2. {
  3. char line[] = "333 hou 23se 444 bi 4g";
  4. char buf[100] = {0};
  5. char *p1 = line;
  6. char *p2 = buf;
  7. while( *p1 )
  8. {
  9. // use temp pointer to locate
  10. // end of digits
  11. char* p3 = p1;
  12. while( isdigit(*p3) )
  13. p3++;
  14. // is there a space following the digits ?
  15. if( *p3 == ' ')
  16. // yes, then skip the space and move on to the next char
  17. p1 = p3+1;
  18. else
  19. {
  20. // no, then was the last char put into temp buffer
  21. // a space
  22. if( *(p2-1) == ' ')
  23. // yes, then overwrite that space with a new character
  24. p2--;
  25. p1 = p3;
  26. }
  27. // copy all characters up to the next digit
  28. while(*p1 && !isdigit(*p1) )
  29. *p2++ = *p1++;
  30. }
  31.  
  32. return 0;
  33. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 22
Reputation: Onixtender is an unknown quantity at this point 
Solved Threads: 0
Onixtender Onixtender is offline Offline
Newbie Poster

Re: C working with string problem

 
0
  #4
Apr 27th, 2008
Hi,
Firstly,thanks for great idea!
Secondly, sorry that I didn't replied earlier...
Anyway I have one more question : if for egz.: we are reading multiple lines from text,
and we want to print them to the monitor, because of this code transformed lines are moved to buf and then printed, then moved to buf and then printed so we get
(input)
:333 hou 23se 444 bi 4g
333 hou 23se 444 bi 4g
333 hou 23se 444 bi 4g
(output):
house big
house bighouse big
house bighouse bighouse big
//-------------------------------------------
char buf[100] = {0};
char *p1 = line;
char *p2 = buf;

So how to free the memory of buf or exactly *p2?(that we could put a new line in it)
, free(buf); don't work... if *p2='\0'; then nothing again...
Well about lines (from above page 18,19), it was somehow connected with making a line with which we could work...
But probably it would be better if program would read everything into a line like
333 hou 23se 444 bi 4g\n333 hou 23se 444 bi 4g\n333 hou 23se 444 bi 4g\n
and then work with it?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 979
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 209
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: C working with string problem

 
0
  #5
Apr 27th, 2008
You could turn Ancient Dragon's code into a function and then use it to process your input file line-by-line. It could look something like (the function is here AD())
  1. void AD(char * record, char * record_out)
  2. {
  3. char *p1 = record;
  4. char *p2 = record_out;
  5.  
  6. // Rest of the original code here ...
  7. }
  1. // In your main(), the while loop
  2. while (fgets(record, sizeof(record), fin))
  3. {
  4. // A buffer local to the while() loop, of same size as the record array
  5. char record_out[sizeof(record)] = {0};
  6.  
  7. // Process the line
  8. AD(record, record_out);
  9.  
  10. // Display what we got
  11. puts(record_out);
  12. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 22
Reputation: Onixtender is an unknown quantity at this point 
Solved Threads: 0
Onixtender Onixtender is offline Offline
Newbie Poster

Re: C working with string problem

 
0
  #6
Apr 27th, 2008
Thanks a lot , everything works just fine
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC