-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;
}
Cboehm
  • 43
  • 1
  • 8
  • How are you inputting an empty string? And what result exactly do you get? – kaylum Feb 21 '16 at 09:54
  • 'when I enter a blank string my program doesn't print to screen correctly' - what does it do? – Martin James Feb 21 '16 at 09:55
  • Well, this is weird: I run the program online [here](http://www.tutorialspoint.com/compile_c_online.php) 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. – Box Box Box Box Feb 21 '16 at 09: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([|[][[) – Cboehm Feb 21 '16 at 09:58
  • Why is `MyStrlen` returning `int *` instead of `int`? Didn't the compiler emit some warnings for that? – kaylum Feb 21 '16 at 10:00
  • Enter a string: strlen(''╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠┌>9┘£√▬'') returned 111 MyStrlen(''╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠┌>9┘£√▬'') returned 111 Press any key to continue . . . this is my output. when I enter any other character except a blank It works – Cboehm Feb 21 '16 at 10:02
  • OK, null-terminator, next.. – Martin James Feb 21 '16 at 10:10
  • 1
    all of the `&string2` in main should be `string2` – M.M Feb 21 '16 at 10:24
  • did it. Still didin't change anything – Cboehm Feb 21 '16 at 10:27

2 Answers2

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
Community
  • 1
  • 1
Gomiero
  • 2,240
  • 2
  • 22
  • 18
  • this exact code is not working for me. It is still giving me the same output. – Cboehm Feb 21 '16 at 10:22
  • @Cboehm change the scanf line to `if ( 1 != scanf("%100[^\n]", &string2[0]) ) { string2[0] = 0; }` – M.M Feb 21 '16 at 10:27
  • 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? – Gomiero Feb 21 '16 at 10:34
  • 1
    I am using Visual studios and not necessarily still testing. @Gomiero – Cboehm Feb 22 '16 at 00:40
-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.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
  • oh thanks I still get the same output though when I run it. – Cboehm Feb 21 '16 at 10:08
  • Well, i get proper output making these changes. I'll post a screenshot to show you. – Box Box Box Box Feb 21 '16 at 10:10
  • 2
    the printf statements have wrong 2nd argument, although you make a good point about the blank string – M.M Feb 21 '16 at 10:24
  • @AshishAhuja the `-1` is in the third argument – M.M Feb 21 '16 at 10:30
  • @M.M, just got confused, edited. Well, I know that `&` returns address, but it still works with the `&`. Do you know why? – Box Box Box Box Feb 21 '16 at 10:32
  • 3
    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) – M.M Feb 21 '16 at 10:33
  • 2
    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` – M.M Feb 21 '16 at 10:34
  • @M.M, will do. Just, for clarification, I have just copied the `scanf`, and will not change it as I am telling the OP not to do that, and instead do the `fgets`. – Box Box Box Box Feb 21 '16 at 10:36
  • I see, that's fine then – M.M Feb 21 '16 at 10:42
  • @M.M, is everything alright now. It works completely. Any other mistake? If not, can you please undo the downvote. – Box Box Box Box Feb 21 '16 at 10:48
  • 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 `=`. – M.M Feb 21 '16 at 11:03