compare argv with char[ ] problem

Hi guys, could you please help me with this code:
I need to compare argv with char text[], if argv[1] (for example: if I set ./program a via command line) i need to replace all 'a' in text with 'e'.

input: "stamp palm lamb america latino karate"
output: stemp pelm lemb emerice letino kerete

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
char text[] = "stamp palm lamb america latino karate";
char buf[100];
char filename[30];
int l = strlen(text);
strcpy(filename, argv[1]);
int i;
for(i=0; i < l; i++)
{
if(text[i] = [1])
buf[i] = 'e';
else
buf[i] = text[i];
}

for(i=0;i<l;i++)
{
printf("%c",buf[i]);
}
printf("\n");
return(0);
}
I need to compare argv with char text[]
bool same = strcmp(argv[1], text) == 0;

i need to replace all 'a' in text with 'e'.
You can use strchr to look for a character in a string.

Please note I've edit the code above.
Last edited on
I tried somethin like this:
But because I'm total beginner in programming could you please help more. What loop I have to use to move char by char to change the letter of the string with this strchr function?

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
int i = 0;
char text[] = "stamp palm lamb america latino karate";
int l = strlen(text);
char buf[100];
char *pos = strchr(text, 'a');
if (pos)
printf("Character 'e' found at position %d.\n", pos - text);
return(0);
}
As you've written, this finds 'a'
 
char *pos = strchr(text, 'a');

pos is NULL (or zero) if 'a' is not found. So you need to check for zero. pos otherwise points to 'a'.

You can assign a new character to that position with:
1
2
3
4
if (pos)
{
    *pos = 'e';
}

This means assign 'e' to the place where pos points to.

You need to put it in a loop and keep going until pos is NULL (zero).

Is there a reason you're using C strings rather than C++ strings in a C++ program?
Last edited on
Topic archived. No new replies allowed.