-2

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;
}
9
  • How are you inputting an empty string? And what result exactly do you get? Commented Feb 21, 2016 at 9:54
  • 'when I enter a blank string my program doesn't print to screen correctly' - what does it do? Commented Feb 21, 2016 at 9:55
  • Well, this is weird: I run the program online here and I get perfect output even for blank string, but when I run it my terminal on Ubuntu 14.04 using gcc, I get totally wrong output. Commented Feb 21, 2016 at 9:57
  • I have been instructed to use something in the sense of buf[strlen(buf) - 1] = '\0'; but it gives me nothing relavant. It runs until 111 letters and is a bunch of different symbols([|[][[) Commented Feb 21, 2016 at 9:58
  • 1
    all of the &string2 in main should be string2 Commented Feb 21, 2016 at 10:24

2 Answers 2

3

Maybe the problem could be corrected with:

Initilize the first element of string2 to '\0' before the scanf:

string2[0] = '\0';

Change the return type of int *MyStrlen(...) to int:

int MyStrlen(const char *string2);

As this post: How to input a string using scanf in c including whitespaces, a safer way is to specify a size 1 less than the size of string2 buffer:

scanf("%99[^\r\n]", &string2[0]);

Check out the scanf man page for more information.

The code:

#include<stdio.h>
#include<conio.h>
#include<string.h>

int MyStrlen(const char *string2);

// change the return type to int
int MyStrlen(const char *string2)
{
    int stringcount=0;

    while (string2[stringcount] != '\0')
        stringcount++;
    return stringcount;
}
int main()
{
    char string2[100];

    // Initialize the first element to 0
    string2[0] = '\0';
    printf("Enter a string: \n");

    scanf("%99[^\r\n]", &string2[0]);

    int  length, length2;


    length =  strlen(string2);
    length2 = MyStrlen(string2);

    // Change to &string2 to string2
    printf("strlen(''%s'') returned %d\n", string2, length);
    printf("MyStrlen(''%s'') returned %d\n", string2, length2);

    return 0;
}

The output with empty string:

Enter a string:

strlen('''') returned 0
MyStrlen('''') returned 0

The output with "aaa":

Enter a string:
aaa
strlen(''aaa'') returned 3
MyStrlen(''aaa'') returned 3
Sign up to request clarification or add additional context in comments.

4 Comments

this exact code is not working for me. It is still giving me the same output.
@Cboehm change the scanf line to if ( 1 != scanf("%100[^\n]", &string2[0]) ) { string2[0] = 0; }
Tks @M.M :) @Cboehm I'm using MinGW-64 gcc on Windows to make the test. Which compiler are you using? Does it work now?
I am using Visual studios and not necessarily still testing. @Gomiero
-1

Well, the problem is with

scanf("%100[^\n]",&string2);

Change it to

fgets (string2, 100, stdin);

Because that scanf cannot read a blank string.

Also, as mentioned in @M.M's comment, check for '\n':

int last = strlen (string2) -1;
if (string2 [last] = '\n') {
   string2 [last] = '\0';
}

enter image description here

In the second case I entered a blank string.

The completely corrected code:

#include<stdio.h>
#include<conio.h>
#include<string.h>

int *MyStrlen(const char *string2);

// change the rerturn type to int
int *MyStrlen(const char *string2)
{
    int stringcount=0;

    while (string2[stringcount] != '\0')
        stringcount++;
    return stringcount;
}
int main()
{
    char string2[100];

    // Initialize the first element to 0
    string2[0] = '\0';
    printf("Enter a string: \n");
    fgets (string2, 100, stdin);
    int  length, length2;
    int last = strlen (string2) -1;
    if (string2 [last] = '\n') {
       string2 [last] = '\0';
    }


    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 as mentioned in @Gomiero's answer, initialize your string to \0.

11 Comments

oh thanks I still get the same output though when I run it.
the printf statements have wrong 2nd argument, although you make a good point about the blank string
undefined behaviour can do anything. But you are probably on a system where &x and x have the same size and representation if x is an array. (in general this may not be true)
The scanf should not have & either. The fgets function does not always append \n (e.g. if input overflowed or input closed) so you should really check for the presence of the \n and remove it if present, instead of blanket -1
strlen (string2) -1 is incorrect as the result of strlen may be 0. (so, as well as accessing out of bounds on the next line, you have a signed-unsigned mismatch). Also, the line after that should have == rather than =.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.