So I am writing this code to print string length of any string I input and I basically have the code already working but I am having trouble because when I enter a blank string my program doesn't print to screen correctly. It works with a space(spacebar) and all other strings but I must be able to enter an empty string. we are supposed to use something like: buf[strlen(buf) - 1] = '\0'; to enter and print empty strings, but I am not sure how to enter it in the code correctly. Any ideas??
here is my program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int *MyStrlen(const char *string2);
int main()
{
char string2[100];
printf("Enter a string: \n");
scanf("%100[^\n]",&string2);
int length, length2;
length = strlen(string2);
length2 = MyStrlen(string2);
printf("strlen(''%s'') returned %d\n", &string2, length);
printf("MyStrlen(''%s'') returned %d\n", &string2, length2);
return 0;
}
also, here is MyStrlen function, All works correctly besides entering empty string.
int *MyStrlen(const char *string2)
{
int stringcount=0;
while (string2[stringcount]!='\0')
{
stringcount++;
}
return stringcount;
}