I intend to modify each other letter of a particular string. But for the purposes of this program none of that occurs. So far I've grabbed a string from the user and stored it in userinput
and intend to print it.
#include <stdio.h>
#include <string.h>
int main(void) {
char userinput[256] ="";
printf("Enter somthing to change:\n");
scanf("%s", &userinput);
printf("%s\n", userinput);
int k = 2; // This is just here to do something every time k is even
int j = strlen(userinput);
for (int i = 0; i < j; i++) {
if(k % 2 == 0) {
printf("%s", userinput[i]);
k++;
}
else {
printf("%s", userinput[i]);
k++;
}
}
}
The strlen()
function however does not work on the userinput
. I figure this is because strlen()
is supposed to take the address of the first char of a string and then iterate until reaching a null char but scanf doesn't actually create a null char.
I couldn't figure out a way of adding the '\0'
after the string without first knowing the length of the string.
How would I go about accessing the length of a stored character sequence if it's stored in an array?
You're confused about types, as others have indicated. Using scanf and printf when you're confused about types is dangerous!
The type of
&userinput
ischar (*)[256]
, but scanf expects%s
to correspond to achar *
. Since the type expected and the type given differ and aren't required to have the same representation, the behaviour is undefined.Wrong. scanf certainly does assign a null character, when you use the
%s
format specifier. That is, providing scanf doesn't encounter an error or EOF. On that note, you should probably check for errors from scanf:... as you should with all standard library functions in C.
k
is pointless. Your loop already incrementsi
at the same frequency ask
.strlen returns a
size_t
. Make sure you store return values in the right types. If a function returnssize_t
, then you store the return value insize_t
. If a function returnsint
, then you store the return value in anint
. Got it? Whatever the return type is, is whatever type you use to store the return type. Use the manual to find that out.There's that type confusion again.
userinput[i]
is achar
.%s
tells printf to expect achar *
. When the argument is invalid, the behaviour is undefined. That may cause your program to malfunction. Considerprintf("%c", userinput[i]);
orprintf("%s", &userinput[i]);
.Change this
with
we have to use addresses for
scanf
:int a
then we have to callscanf()
with the address ofa
=>&a
double a
then we have to callscanf()
with the address ofa
=>&a
int *a;
or char arraychar a[50];
then we have to callscanf()
with the pointer or with the array without adding&
From the scanf() page
This:
should be:
The address of operator
&
is unrequired, and incorrect. Arrays decay to the address of their first element when passed to a function.scanf("%s")
will append a null terminating character so it is unnecessary to explicitly insert one.To prevent potential buffer overrun specify the maximum number of characters that
scanf()
should write touserinput
. This should be one less than the size ofuserinput
, leaving room for the terminating null character:The incorrect format specifier (which is undefined behaviour) is being used to print the
char
acters ofuserinput
: use%c
not%s
. This:must be:
Change
to
The
&
operator is not required in the case of String capture. Thescanf()
automatically appends the null character('\0'
) to the end of the string soint j = strlen(userinput);
should work.If you still want to calculate the length without this function efficiently here is the link How to calculate the length of a string in C efficiently?