Thread: Arrays of Strings are fun

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    81

    Arrays of Strings are fun

    Okay, I have 2 functions I am trying to create. The first, will read in a character string array of numbers and insert dashes into it(like in a social security number or a phone number).

    The second one will read in a string of characters in an array and cut-off one of the names by inserting the delimiter('\0')

    However, I am having problems doing both. Any suggestions would be great.

    Code:
    void dashinsert(str x[])
    {
    for(int i=0;i<23;i++)
    {for(int j=0;j<9;j++)
    cout << x[j] << endl;
    }
    
    cout << x[4];
    
    }
    
    void cutoff(str x[])
    {
    //read-in array
    	//go through and insert '/0' after the first letter of middle name
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    6
    I don't really understand your problem...

    the code you give isn't useful and your explanations aren't very clear. What is str ? why don't you use string ? they provide several functions to help you

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Your code should probably look like "char str[]" or "char x[]"

    Inserting a null to "end" the string...

    First, use a loop to search for a space. x[n] == ' ';
    Code:
    char x[] = DougDbug;
    x[4] = '/0';    // Replace the second 'D' with '\0' So that "Doug" will print-out.
    Inserting hyphens...

    First, you have to shift-right all the characters right of the hyphen.

    So, again in a loop (starting at the end and working backwards).
    Code:
    ssn[4] = ssn[3];  // Move the character in position 3, to position 4
    ssn[3] = '-' ;         // Overwrite position 3 (4th character) with a hyphen.
    [EDIT]
    BTW - What you are working with is a "character array" or "c-style string". An example of an "array of strings" would be a an array of sentances, which the computer would see as a two-dimensional array. Or, "an array of character-arrays".
    Last edited by DougDbug; 07-01-2003 at 02:35 PM.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    thanks for your help, I'll give it a shot

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Another suggestion - start small.

    First, get the '\0' program working on your name without a loop, and without looking for the space.

    Once that works, add the loop with the space-search.


    For the hyphen, start by shifting the last character (no loop).
    123456789 // Input
    1234567899 // Output (shift last character)

    Once, you have that working, make a loop to do this:
    123456789 // Input
    1234566789 // Output (shift last 4 characters)

    Then:
    123456789 // Input
    12344566789 // Output

    Finally, over-write the characters where the hyphens belong.
    123456789 // Input
    123-45-6789 // Output

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    Okay, I tried some things but I am still having problems. the only function I have enabled is the dashinsert one and its spitting out garbage numbers. Do I need to change the ss[] array to a char array and what I have? When I dothat it gives me an error. Here is my code:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    
    //Global variables
    ifstream infile;
    ofstream outfile;
    
    typedef char str[30];
    
    //function prototypes
    void tocaps(str []);
    void dashinsert(char []);
    void cutoff(str []);
    
    int main()
    {
    
    char ss[30],ph[30];
    str name[30];
    
    
    	infile.open("in599A.dat");
    	while(!infile.eof())
    	{
    		for(int i=0;i<23;i++)
    		{
    		infile >> ss[i];
    		infile >> ph[i];
    		infile.getline(name[i],30);
    		}
    		
    		//cout << ss[i] << endl << ph[i] << endl;
    		//cout << name[i] << endl;}
    		
    		
    		//change to caps
    		     //tocaps(name);
    		     //cout << "after caps function" << endl;
    		      //for(i=0;i<23;i++)
    		      //cout << name[i] << endl;
    		//take off the last characters of middle name
    
    		      //cutoff(name);
    		//compare to second file
    
    		//add dashes to social security number and add dashes to phone number
    		     dashinsert(ss);  // social security dash insertion
    		     //dashinsert(ph);  // phone number dash insertion
    		//sort
    			  
    		//binary search
    
    	}	
    	return 0;
    }
    // to conversion to capital letters
    void tocaps(str name[])
    {
    	for(int r=0;r<23;r++)
    		{
    			for (int c=0;c<strlen(name[r]);c++) {
        name[r][c] = toupper( name[r][c] );
    		}
    		
    }
    
    }
    
    void dashinsert(char x[])
    {
    //for(int i=0;i<23;i++)
    //{for(int j=0;j<9;j++)
    
    x[12] = x[8]; 
    x[3] = '-' ; 
    for(int i=0;i<23;i++)
    cout << x[i] ;     
    }
    
    void cutoff(str x[])
    {
    //read-in array
    	//go through and insert '/0' after the first letter of middle name
    //look for first space then look for another space then add /o after
    
    }

  7. #7
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Your typedef is wrong. You want char*, not char.

    Look, here's the same program using the standard string class:
    Code:
    void dashinsert(std::string& x) {
      x.insert(5,"-");
      x.insert(3,"-");
    }

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    honeslty, I haven't learned the standard string class thing(x.insert(5,"-") yet. My class just started on pointers... so, I'm a little new to it. Does it look like that is what I need to use?

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    6
    yes, your program would be much simpler with string. If you can, learn it and use it

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    can't learn it, teacher won't let me

    if you'd like to help out on my other problems please visit
    http://cboard.cprogramming.com/showt...threadid=41423

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help understanding arrays of strings!
    By smoking81 in forum C Programming
    Replies: 18
    Last Post: 02-23-2008, 04:24 AM
  2. Fun with arrays..
    By icedtea in forum C Programming
    Replies: 6
    Last Post: 09-27-2006, 10:10 AM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM
  5. Searching arrays for strings
    By Zaarin in forum C++ Programming
    Replies: 14
    Last Post: 09-03-2001, 06:13 PM