How to search through agrv[1]

Hello,

I've been stuck on this part for quite some time now. Could someone please point me in the direction of how to search through agrv[1] so that i can change the numbers into a letter? Thanks in advance.


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
 # include <iostream>
#include <stdlib.h>
#include <cstring>

using namespace std;

int main(int argc, char *argv[])
{
	int num;
	
	const int size = 200;
	char array[size];

	if (argc != 2) {
		cout << "Error" << endl;
		return 0;
	}
	
	int counter;
	if (argc == 2)

	for (char i = 0; i < strlen(argv[1]); i++)
		{
		if (array[i] = 2)
			array[i] = 'a';
		cout << array[i];
		
		}
			
	return 0;
}
Last edited on
you can drop it to a string:

string s = arv[1];
...etc

or you can go C on it:
int len = strlen(argv[1]);
for(x = 0; x < len; x++)
{
do things here, probably best to just copy the stuff you want into a new string or c-string based off these inputs.
if (s[x] == 1)
result[x] = 'a'; //example...
}




line 24 if (array[i] = 2) I guess you mean if (array[i] == 2)
For the conversion it would be better to use a lookup table - much easier then long if or switch statements
1
2
3
4
5
6
7
8
9
10
11
12
  char replace_table[] = "aeiou ";
  // ...
  int len = strlen(argv[1]);
  for (int i = 0; i < len; i++)
  {
    int num = argv[1][i] - '0'; // convert char to an int
    if (num >= 1 && num <= 6)
    {
      argv[1][i] = replace_table[num - 1];
    }
    // else leave orig char
  }
@Thomas1965, you can avoid std::strlen() by testing your for-loop for the null-character (but, ok, it can reduce readability).
@Enoizat,
you are right. Just used strlen because the OP also used it, so he/she might better understand.
Topic archived. No new replies allowed.