Reputation: 3
I'm studying for an exam and I need some help with strings
Assume the following declarations, and further assume that string.h is uncluded
char rocky[21], bw[21], boris[21];
int result;
a.) Write a scanf statement that would enable the string Beauregard to be read into rocky
my answer= scanf("%s", &rocky);
b.) Assuming that the text Beauregard is the only thing on th eline of standard input, write a statement to read in the text and store it in rocky using an alternative to scanf
my answer= gets (Beauregard);
strcpy(rocky);
c.) assuming that the text read in is Beauregard, what is the value of result after the following statement is executed?
result=strlen(rocky);
my answer= i have no clue..
d.) what does the following statment do?
strcpy(boris, rocky);
answer= makes a copy of the string..(dont know much more than that)
e.) what does the following statement do? What are the values of rocky and bw?
strncpy(bw,rocky,3);
my answer= not a clue
help is much appreciated, and an explanation would also help :)
Thanks!
Upvotes: 0
Views: 125
Reputation: 3870
a. Arrays and pointers are closely related in C. In particular, the name of an array decays to a pointer to the first element of the array, so your answer should be
scanf("%s", rocky); /* note the lack of an & in front of rocky */
b. gets(Beauregard)
doesn't really make sense. The gets function reads a string from standard input (think, "keyboard") and stores it in the character array pointed to by the argument you pass it. So you're supposed to assume the user will type "Beauregard", and you should read it into the rocky
array with
gets(rocky);
c. strlen returns the length of the string, not including the trailing \0
character, so in this case, 10.
d. strcpy just copies the contents of the rocky array into the boris array, so they'd both contain "Beauregard".
e. strncpy works like strcpy, but only copies up to n characters (where n is the last argument, so in this case, bw would contain "Bea" without a terminating null character.
Note that several of these statements are really bad ideas in any real program. There is never a reason to use gets, for example, as any use of gets opens up security flaws. You should always use fgets instead. The scanf function can be used safely if you specify the width, but you haven't done so here. I mention these things just in case your teacher has covered them and you've forgotten.
Upvotes: 1
Reputation: 1587
c.) strlen counts until it reaches the terminating null character, \0
. So the answer would be 10, because there are 10 letters in Beauregard.
Strlen
d.) Yes, it copies a string. More specifically, it copies rocky into boris. I'm not sure what else they would want you to give as part of an answer there...
e) It copies the first 3 characters of rocky into bw. However, it does NOT add a terminating null character. strncpy
Upvotes: 0
Reputation: 7951
strlen(rocky) is going to return the string length of what is pointed to by rocky. The number of letters that make up 'Beauregard'.
strncpy(bw, rocky, 3) copies the first 3 letters from the string pointed to by rocky into bw.
You should read man pages for strlen, strcpy and strncpy.
Upvotes: 0
Reputation: 75130
strlen
returns the length of the C string. strlen("Beauregard");
would return 10
because the string is 10 characters long.
strcpy
just copies a string, you're right.
strncpy
allows you to specify the maximum number of characters you want. So if you pass it 3
, you'll get 3 characters and the null terminator on the end of your string.
Upvotes: 1