Thread: Help with strings

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    4

    Question Help with strings--ANOTHER QUESTION

    I have an assignment where I need to scan in strings from a data file (there are about 12 strings). the strings have random numbers in the middle of the words and random punctuation. i need to reprint the strings in reverse order (bottom string at the top, top string at the bottom), and remove all the numbers and punctuation.
    Is it possible to scan a string in as a %s, or do I need to scan each character and assign them to the values of a string?
    thanks.
    Last edited by andy84; 12-01-2004 at 01:38 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Use fgets:

    Code:
    char buffer[20];
    FILE* fpInput;
    ...
    fgets(buffer,sizeof(buffer),fpInput);
    Of course you'll need a character array for each line from the file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I'd recommend using fgets to obtain the strings. I found recursion based on the success of a call to fgets an easy way to reverse the order. And the function isalpha in a simple loop through the string would be useful in determining whether or not to putchar a character.
    Last edited by Dave_Sinkula; 12-01-2004 at 12:05 PM. Reason: D'oh! Pokey again!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    4

    Question

    this is what i have so far. It is just printing random symbols to the output file.
    Code:
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    #include <stdio.h>
    
    #define FILENAME "D:h4ot.txt"
    #define INNAME "D:dat4.txt"
    #define STRINGSIZE 50
    
    int main( void)
    {
    char string1 [STRINGSIZE];
    char string2 [STRINGSIZE];
    char string3 [STRINGSIZE];
    int k;
    
    
    FILE* outfile;
    outfile = fopen( FILENAME, "w" );
    
    
    FILE* infile;
    infile = fopen ( INNAME, "r");
    
    while((fgets( string1, STRINGSIZE, infile)))
    {
    	for (k=0; k<=(strlen(string1)-2); k++)
    		{
    		if (isalpha(string1[k]))
    			{
    			putchar(string2[k]);
    			}
    		}
    strcat(string2, string3);
    strcpy(string3, string2);
    }
    fprintf(outfile, "%s", string3);
    
    return 0;
    }



    thanks

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Let's trim this up a bit and start with something workable.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main( void)
    {
       FILE* file = fopen ( "file.txt", "r" );
       if ( file )
       {
          char line [ 50 ];
          while ( fgets( line, sizeof line, file ) )
          {
             int k;
             for ( k = 0; line [ k ] != '\0'; ++k )
             {
                if ( isalpha ( line [ k ] ) )
                {
                   putchar ( line [ k ] );
                }
             }
             putchar ( '\n' );
          }
          fclose ( file );
       }
       return 0;
    }
    It's much the same as your attempt, but ask questions about any of my changes that you didn't understand. If you need the output to go to a file, use fputc instead of putchar.

    You'll notice that the order is not reversed. This is why I mentioned recursion.

    [Sorry for the name changes, I'm a little crusty. And you'll need to change your filename back.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by andy84
    this is what i have so far. It is just printing random symbols to the output file.
    Code:
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    #include <stdio.h>
    
    #define FILENAME "D:h4ot.txt"
    #define INNAME "D:dat4.txt"
    #define STRINGSIZE 50
    
    int main( void)
    {
    char string1 [STRINGSIZE];
    char string2 [STRINGSIZE];
    char string3 [STRINGSIZE];
    int k;
    
    
    FILE* outfile;
    outfile = fopen( FILENAME, "w" );
    
    
    FILE* infile;
    infile = fopen ( INNAME, "r");
    
    while((fgets( string1, STRINGSIZE, infile)))
    {
    	for (k=0; k<=(strlen(string1)-2); k++)
    		{
    		if (isalpha(string1[k]))
    			{
    			putchar(string2[k]);
    			}
    		}
    strcat(string2, string3);
    strcpy(string3, string2);
    }
    fprintf(outfile, "%s", string3);
    
    return 0;
    }
    thanks
    Of course it does. What did you expect it to do? First off, your buffer isn't initialized, so it has a bunch of random crap in it. Next, the first time string3 shows up in your program, you're copying it to another uninitialized buffer, and then copying that back over top of it. Then you print it to a file?

    How could it possibly contain something useful?

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    Dave_Sinkula , i copied your program into Turbo C++ (my compiler...it also does plain C) and created a data file called file.txt which is in the same directory as the program.
    the data file looks like this:

    asdf44sdf
    asdfa44sdfsdfa
    asd45fsadwe
    ewweeer435rdf
    fddfd55fdfdf

    When the program runs, it doesn't come up with any errors, but it doesn't take out any of the numbers like it's supposed to either.





    quzah :
    I don't understand what you mean about initializing the buffer. are you saying that i should fill the string1 with all zero's? the examples in my book do it like i did, just declaring a char string1[50]

    thanks for you help so far.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by andy84
    When the program runs, it doesn't come up with any errors, but it doesn't take out any of the numbers like it's supposed to either.
    With this "file.txt":
    Code:
    Pete25.33Moss
    Sara78.45Bellum
    Harry76Legg
    Les34.5Ismoore
    April109Showers
    June87.345Meadows
    Mae68.5Flowers
    Guy56Wire
    Chris66.335Craft
    Slim115.25Pickens
    I get this output:
    Code:
    PeteMoss
    SaraBellum
    HarryLegg
    LesIsmoore
    AprilShowers
    JuneMeadows
    MaeFlowers
    GuyWire
    ChrisCraft
    SlimPickens
    And for me it removes the numbers from your example as well.
    Code:
    asdfsdf
    asdfasdfsdfa
    asdfsadwe
    ewweeerrdf
    fddfdfdfdf
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    4

    Question

    ok, i figured that out a minute ago. everything is working now, i just need to flip the strings around (so the first one read in is at the bottom of the page, last one read in is at the top).

    this is what i have so far:
    Code:
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    #include <stdio.h>
    
    #define FILENAME "D:h4ot.txt"
    #define INNAME "D:dat4.txt"
    
    
    int main( void)
    {
    char string1 [50];
    
    
    
    int k;
    
    
    FILE* outfile;
    outfile = fopen( FILENAME, "w" );
    
    
    FILE* infile;
    infile = fopen ( INNAME, "r");
    
    while(fgets( string1, sizeof(string1), infile))
    {
    	 for ( k = 0; string1 [ k ] != '\0'; ++k )
    		{
    
    		if (isalpha(string1[k]))
    			{
    			fputc(string1[k], outfile);
    			}
    
    		else
    			{
    			switch (string1[k])
    				{
    				case ',':
    				case '.':
    					 fputc(string1[k], outfile);  break;
                                    default:  
    					fputc(' ', outfile);
    					}
    			}
    		}
    
    		fputc('\n', outfile);
    
             
    
    		
    
    }
    fprintf(outfile, "%s", string1);
    
    return 0;
    }

    this is what i am thinking: have a string2 that i can concatenate (strcat) string 1 to. then i keep adding string 2 to the end of string1 each time. The only problem is that i don't know how to declare or start the string2 so that it has 50 spaces that are empty.

    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM