Say I have:
....
char aLine;
char inputLine[1000];
scanf("%c", &aLine);
....
Now, I want to convert aLine into an array of char so that I can count how many characters in aLine. How can i do this?
Say I have:
....
char aLine;
char inputLine[1000];
scanf("%c", &aLine);
....
Now, I want to convert aLine into an array of char so that I can count how many characters in aLine. How can i do this?
Statement scanf("%c",...
reads in a single character, so there is actually no need to count it. If you want to "convert" a single character into a string (for which you could then apply, for example, strlen()
), write the character at a particular position. Don't forget to terminate the string with \0
(or 0x0
) such that string functions like printf("%s"...
or strlen()
work correctly:
char aLine;
char inputLine[1000];
scanf("%c", &aLine);
inputLine[0] = aLine;
inputLine[1] = '\0';
printf("inputLine: '%s' has length %d", inputLine, strlen(inputLine));
or simply read in a complete line at once:
char inputLine[1000];
if (fgets(inputLine,1000,stdin)) {
printf("inputLine: '%s' has length %d", inputLine, strlen(inputLine));
}