Thread: Help Please. Char Arrays

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    19

    Help Please. Char Arrays

    hi, i have to write a program that converts roman numerals to regular numbers and it takes all the info from the commandline, like so: "% roman MMIV".
    here is what takes the input.
    Code:
    int main(int argc, char* argv[])
    how can i take argv[] and store anything the user puts in into another array.

    for example, let's say the user inputs "roman MCI".
    how can i make an array that goes through each letter with a for loop and prints out each letter separately.
    like
    M
    C
    I
    help please.

    edit: i only need help on this for now. i'll work on the rest by myself. thanks

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    assuming that MMCI was the first parameter to the program.
    Code:
    char buffer[10];
    int count=0;
    
    for (count=0; argv[1][count] != '\0' && count < 10; count++)
      buffer[count] = argv[1][count];

  3. #3
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    	char *pNumerals;
    	int i;
    	
    	/* check for correct number of command-line arguments */
    	if (argc != 2)
    	{
    		exit(1);
    	}
    	
    	/* allocate memory and copy string from argv */
    	pNumerals = malloc( (strlen(argv[1])) + 1);
    	strcpy(pNumerals, argv[1]);
    	
    	/* loop through the characters of pNumerals and print */
    	for (i = 0; i <= strlen(pNumerals) - 1; i++)
    	{
    		printf("%c\n", *(pNumerals + i));
    	}
    	
    	/* deallocate the memory allocated to pNumerals */
    	if (pNumerals != NULL)
    	{
    		free(pNumerals);
    	}
    	
    	return 0;	
    }

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    19
    Quote Originally Posted by Thantos
    assuming that MMCI was the first parameter to the program.
    Code:
    char buffer[10];
    int count=0;
    
    for (count=0; argv[1][count] != '\0' && count < 10; count++)
      buffer[count] = argv[1][count];

    thanks that worked.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    19
    hi, how can i get this program to function properly. if "IX" is entered, i get 11 instead of 9. i wanted to write an equation where if the one letter was smaller than the next (IX), then subtract 2 times the value of the first. how can i incorporate that into my program? thanks

    Code:
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
      int ctr,sum=0,count;
      char caro[11];
    
      for (count=0; argv[1][count] != '\0' && count < 11; count++)
        {
          caro[count] = argv[1][count];
    
          switch(caro[count])
            {
            case 'M':
              sum += 1000;
              break;
            case 'D':
              sum += 500;
              break;
            case 'C':
              sum += 100;
              break;
            case 'L':
              sum += 50;
              break;
            case 'X':
              sum += 10;
              break;
            case 'V':
              sum += 5;
              break;
            case 'I':
              sum += 1;
              break;
            default:
              printf("Illegal letter encountered!\n");
              break;
            }
        }
    
      /*
      for(ctr = 0; ctr < 11; ctr++)
        {
          if(caro[ctr] < caro[ctr+1])
            sum -= (2*caro[ctr]);
        }
      */
    
      if(argc > 2)
        printf("Usage: roman <numeral>\n");
      else
        printf("The decimal equivalent of %s is %d\n", argv[1], sum);
    }

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    To do this effectivaly you need to change your algroithm.

    While you are parsing (looking through) argv[0] you need to look at the current character and the next character to see if that one has a larger roman value then the one you are looking at.

    Let me step you through an example:

    XCXIX (not sure if this is a truely valid one but its good enough for a test)

    Step 1) assign a pointer to the first character:
    2) Get value of current character: 10
    3) Look at value of next character: 100
    4) Is 100 > 10? Yes
    5) Since it is we'll take the value of 100 - 10 and add it to our sum.
    6) Move pointer two places
    7) We should be pointing to the second X
    8) Get value of current character: 10
    9) Get value of next character: 1
    10) is 1 > 10? No
    11) add value of current character to sum;
    12) move pointer one character
    13) Now we are pointing at the I
    14) Get value of current character: 1
    15) Get value of next character: 10
    16) Is 10 > 1? Yes
    17) Add the value of 10 - 1 to sum
    18) Move pointer to places
    19) Well now we are pointing to the end so we stop looking at the string

    20) Display the sum

    Also siince you aren't actually using the copied values from argv[1] you should just use a character pointer.

    say you had:
    Code:
    char str[]="XIV";
    You could parse it as such:
    Code:
    char *ptr;
    for ( ptr = str; *ptr != '\0'; ) /* The body will control the ptr position */
    To move the pointer ptr forward one position it would be
    Code:
    ptr++;
    and to move two positions
    Code:
     ptr += 2;
    Hope this helps
    Last edited by Thantos; 05-28-2004 at 09:45 AM. Reason: fixed typo with closing code tag

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM