Command line paramters

Hi, I'm trying to parse, or tokenize strings that follow the program name/command when used command line. I want to use the command followed by a sentence with a period. I know I need to user argv. But how can i parse the command rather than prompting the user for input. The input will be a sentece.Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <sched.h>

#define MAX_SIZE 50
using namespace std;

//function prototypes
void *vowels(void*);
void *constants(void*);

//global vars
string strings1[MAX_SIZE];
string strings2[MAX_SIZE];
string strings3[MAX_SIZE]; 
int numWords = 0;

int main(int argc, char* argv[]) 
{
	pthread_t vow;
	pthread_t con;
	
	char input[MAX_SIZE];
	char input2[MAX_SIZE];
	char* tokenize;
	int any;
	
	//prompt use to enter a string
	printf("Please enter a string.\n");
	
	cin.getline(input,MAX_SIZE,'.');

	tokenize = strtok(input, " ");

	while(tokenize)
	{			
		strings1[numWords] = tokenize;		
		strings2[numWords] = tokenize;
		strings3[numWords] = tokenize;

		tokenize = strtok(NULL, " ");

		numWords++;
	}
	
//	for(int j = 0; j < numWords; j++)
//		cout << strings1[j] << endl; 
		
	pthread_create(&vow, NULL, vowels, &any);
	pthread_create(&con, NULL, constants, &any);	
	
	pthread_join(con, NULL);
	pthread_join(vow, NULL);	

		
	return 0;
}
void *vowels(void* any)
{
	int position = 0;

	while( position < numWords )
	{
		string temp = strings1[position]; //store the current string into temp
	
		//check for vowels lower and upper
		if(temp[0] == 'a'|| temp[0] == 'e'|| temp[0] == 'i'|| temp[0] == 'o'
		|| temp[0] == 'u'|| temp[0] == 'A'|| temp[0] == 'E'|| temp[0] == 'I'
		|| temp[0] == 'O'|| temp[0] == 'U')
		{
			printf("vowel: %s\n", temp.c_str());

			position++;

		}
		else
		{
			position++;
			sched_yield();
		}
	}

	pthread_exit(0);	
}

void *constants(void* any)
{
	int position = 0;	

    while( position < numWords )
    {
        string temp = strings2[position]; //store the current string into temp
        
		//Check for constants lower and upper 
        if(temp[0] == 'b'|| temp[0] == 'c'|| temp[0] == 'd'|| temp[0] == 'f'
		|| temp[0] == 'g'|| temp[0] == 'h'|| temp[0] == 'j'|| temp[0] == 'k'
		|| temp[0] == 'l'|| temp[0] == 'm'|| temp[0] == 'n'|| temp[0] == 'p'
		|| temp[0] == 'q'|| temp[0] == 'r'|| temp[0] == 's'|| temp[0] == 't'
		|| temp[0] == 'v'|| temp[0] == 'w'|| temp[0] == 'x'|| temp[0] == 'y'
		|| temp[0] == 'z'|| temp[0] == 'B'|| temp[0] == 'C'|| temp[0] == 'D'
		|| temp[0] == 'F'|| temp[0] == 'G'|| temp[0] == 'H'|| temp[0] == 'J'
		|| temp[0] == 'K'|| temp[0] == 'L'|| temp[0] == 'M'|| temp[0] == 'N'
		|| temp[0] == 'P'|| temp[0] == 'Q'|| temp[0] == 'R'|| temp[0] == 'S'
		|| temp[0] == 'T'|| temp[0] == 'V'|| temp[0] == 'W'|| temp[0] == 'X'
		|| temp[0] == 'Y'|| temp[0] == 'Z')
        {
			printf("constant: %s\n", temp.c_str());

            position++;
        }
		else
		{
			position++;
			sched_yield();
		}
	}

	pthread_exit(0);
}


argc stores number of command line parameters
argv[] stores c-strings which contains said parameters. parameter is a sequence of characters separated by one or more whitespaces.
1st parameter is a program name
So if you run your program as
program.exe this is a string -1
Parameters will be:
argc: 6
argv: {
program.exe
this
is
a
string
-1
}
Last edited on
Yeah I got that but the problem I am having now is that when I parse argv with strtok() I only get the first string. How does argv delimit?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//global vars
string strings1[MAX_SIZE];
string strings2[MAX_SIZE];
string strings3[MAX_SIZE]; 
int numWords = 0;

int main(int argc, char* argv[]) 
{
	pthread_t vow;
	pthread_t con;
	
	char* tokenize;
	int any;
	
	//Expect 2 arguments: the program name followed by a sentence
	if (argc < 2) 
	{ 
        cerr << "Usage: " << argv[0] << "  A sentence and period go here." << endl;
        return 1;
    }

	tokenize = strtok(argv[1], " ");

	while(tokenize)
	{			
		strings1[numWords] = tokenize;		
		strings2[numWords] = tokenize;
		strings3[numWords] = tokenize;

		tokenize = strtok(NULL, " ");

		numWords++;
	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <numeric>
#include <vector>

int main( int argc, char* argv[] )
{
    // get the sentence entered in the command line into a string
    std::string sentence ;
    sentence = std::accumulate( argv+1, argv+argc, sentence,
               [] ( const std::string& a, const char* b ) { return a + ' ' + b ; } ) ;
    if( argc > 1 ) std::cout << sentence.substr(1) << '\n' ;

    // get the tokens entered in the command line into a vector
    std::vector<std::string> words( argv+1, argv+argc ) ;
    for( const auto& w : words ) std::cout << w << '\n' ;
}
Topic archived. No new replies allowed.