Reputation: 619
I'm trying to ask the user to type in a string so I will print the length of the string. My code is built succeeded. However, when I entered a word and pressed 'enter', the program keeps running. I had to enter a second word, then the length of the first string displays. I'm confused at argv[1]. Can someone give me some tips and hint on how to fix this? Thanks in advance for your time.
Please note that I'm not allowed to use any string function.
int main(int argc, char* argv[]){
char* s=argv[1];
char input[256];
s = input;
printf("Please enter a string: ");
scanf("%s\n", s);
int str_length = 0;
while (s[str_length] != '\0')
{
str_length++;
if (s[str_length] == '\0') break;
}
printf("%d\n", str_length);
return 0;
}
Upvotes: 3
Views: 35234
Reputation: 11
This is what you want
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX 100
int main(int argc, char* argv[]){
char tmp[MAX];
printf("Plese enter a string:\n");
scanf("%s", tmp);
printf("the length is %d\n", strlen(tmp));
return 0;
}
The argv and argc is another thing like this: argv[0] = a.exe argv[1] = 12345
int main(int argc, char* argv[]){
printf("the length is %d\n", strlen(argv[1]));
return 0;
}
/*-------------------- Output --------------------
> a.out 12345
the length is 5
------------------------------------------------*/
Upvotes: 1
Reputation: 881323
argv[]
is the array that holds your command line parameters, argv[1]
is the first one (other than the command representation itself in argv[0]
, of course). You assign it to s
then immediately overwrite s
, so it's not really needed here.
The reason you're having to enter two lines is the \n
at the end of your input format. It requires whatever can match the format string followed by a newline, hence the %s\n
is eating your first newline so that scanf
has to go back for another one.
%s
on it's own will fix that problem but introduce another one if what you're after is a full line of input - it will only read up to the first whitespace. For a proper line input function, see here.
It does full line input with protection against buffer overflows and detection/cleanup of lines that were too long, something sorely missing in the scanf("%s")
input method.
In addition, the if
statement within the while
is superfluous here since the while
itself will catch the end of the string, and it makes little sense to have both input
and s
refer to the same array (it would make sense if you changed s
at some point, but that's not happening here).
So, a variant without the advanced line iput function could be as simple as:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char* argv[]) {
char input[256];
int str_length = 0;
printf ("Please enter a string: ");
scanf ("%s", input);
while (input[str_length] != '\0') /* Or consider using strlen() */
str_length++;
printf ("%d\n", str_length);
return 0;
}
If you enter Hello
, you'll see that it prints out 5
immediately. You'll also see 5
if you enter Hello Pax
, which is one reason to choose the advanced input function (the other, very important, reason is to avoid introducing a buffer overflow vulnerability into your program).
Upvotes: 6
Reputation: 2586
argv is used to pass process arguments. this means you can run your program like
a.out 123 abcd
argv[1] will already be assigned to a value of 123. you do not have to read it as you are doing now. in your current code , you are overwriting the reference of s to a new string after assigning it with argv[1].
Upvotes: 2
Reputation: 99094
Just change the scanf
line:
scanf("%s", s);
As you have it (scanf("%s\n", s);
), it requires a sequnce of characters ending in \n
, then another \n
to indicate when the input is finished.
Upvotes: 0