strlen function in C

Iam doing skillrack challenges for more than a year now my question is when i execute the following program iam getting 6 and 5, instead of 5 5 for the input:
bell
hell
(Iam getting correct output when i execute this in netbeans ide but what is the problem with skillrack site ide. Please correct if my program contains any error)

what is the reason for this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    
    char str1[1000];
    char str2[1000];
    int s1,s2;
    
    fgets(str1,1000,stdin);
    fgets(str2,1000,stdin);
    
    s1=strlen(str1);
    s2=strlen(str2);
    
    printf("%d\n%d",s1,s2);

}


In my point of view i think they have added spaces at the end of each string. If thats the case how to find the length of the string without taking spaces into account.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
#include <stdlib.h>

int main()
{   
    int i,j;
    char str1[1000];
    char str2[1000];
    int str1len=0,str2len=0;
    
    fgets(str1,1000,stdin);
    fgets(str2,1000,stdin);

    for(i=0;str1[i]!='\0'&&str1[i]!=' ';++i)
    {
        str1len=str1len+1;
    }
    
    for(j=0;str2[j]!='\0'&&str2[j]!=' ';++j)
    {
        str2len=str2len+1;
    }
    
    
    printf("%d\n%d",str1len,str2len);

}


Still showing the same output.
In my point of view i think they have added spaces at the end of each string. If thats the case how to find the length of the string without taking spaces into account.

The fgets() function will also include the newline character '\n' as part of the string, hence the value 5 rather than 4 for a string such as "bell\n"


As an different idea, instead of using fgets(), you might consider fscanf()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    
    char str1[1000];
    char str2[1000];
    int s1,s2;
    
//    fgets(str1,1000,stdin);
//    fgets(str2,1000,stdin);
    fscanf(stdin,"%999s",str1);
    fscanf(stdin,"%999s",str2);
    
    s1=strlen(str1);
    s2=strlen(str2);
    
    printf("%d\n%d",s1,s2);

}


http://www.cplusplus.com/reference/cstdio/fscanf/
http://www.cplusplus.com/reference/cstdio/fgets/


Or another possibility, remove the trailing newline from the string, for example:
1
2
3
    char * end = strchr(str1,'\n');  // find newline
    if (end)                         // if it is found
        *end = '\0';                 // change to null terminator 

http://www.cplusplus.com/reference/cstring/strchr/

Last edited on
IN addition to being unsafe, fgets() is a real pain to use reliably. As Chervil points out, you usually want to remove the trailing \n. But you always have to check if it's there because the last line in the file might not end in a newline.
zero it?

for(x = maxlen; str[x] != ' '; x--);
and I think you want
str[x+1] = 0; //undo the extra -- in the loop
len = strlen(str);

you could get the value without the last 2 steps, but if you do the last 2 steps you can use all the string functions on the piece you want.

if you KNOW you won't fill your buffer, you can also do something practical but slightly off like this..

char* cp = strstr(s, " "); //assumes user does not put in 4 spaces in a row.
cp[0] = 0;
len = strlen(s); //this is correct now.


Topic archived. No new replies allowed.