Hi!
I have written a function for passing a string into it and finding its length.
When I pass a string it prints a correct value.
But when I take two strings and passing them individual len(str1),len(str2) after scanning two strings it returns the wrong value.
In the first case I passed the each string immediately after scanning each of them.
In the second case I scanned the two strings first and passed them then,I got different
answers.Please help me.
First case input and output-
str1=vijay
str2=rajes
output-5,5
Second case input and output-
str1=vijay
str2=rajes
output-4,5
Ist case--

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

main(){
       int len(char str[]);
       char str1[]="",str2[]="";
       printf("Enter a string= ");
       scanf("%s",str1);
       len(str1);
       scanf("%s",str2);
       len(str2);
       getch();
       }
int len(char str[]){
    int i=0;
    while(str[i]!='\0')
                       ++i;
    printf("%d",i);
}

2nd--

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

main(){
       int len(char str[]);
       char str1[]="",str2[]="";
       printf("Enter a string= ");
       scanf("%s",str1);
       
       scanf("%s",str2);
       len(str1);
       len(str2);
       getch();
       }
int len(char str[]){
    int i=0;
    while(str[i]!='\0')
                       ++i;
    printf("%d",i);
}

Please help me change this program in order to get right thing,and mention the fault in the program.I guess it is becase of enter(\n) button.
Thanks in advance.

Dani AI

Generated

Quick answer: the behavior comes from writing past tiny, compiler-sized buffers and invoking undefined behavior, plus a non-returning len prototype. correctly identified the array-size/overflow root cause and correctly noted the function signature issue. The reliable fix is to allocate adequate buffers, read input safely, and either return a length value or use the standard strlen rather than relying on a custom routine that prints instead of returning.

A simple, safe pattern: give each string an explicit size, use fgets to avoid overflow, strip the trailing newline, and compute lengths with strlen (or return a size_t from a custom function). The example below demonstrates these ideas and removes nonstandard headers like conio.h.

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

int main(void) {
    char s1[100], s2[100];
    if (!fgets(s1, sizeof s1, stdin)) return 1;
    s1[strcspn(s1, "\n")] = '\0';
    if (!fgets(s2, sizeof s2, stdin)) return 1;
    s2[strcspn(s2, "\n")] = '\0';
    printf("%zu, %zu\n", strlen(s1), strlen(s2));
    return 0;
}

Additional tips: compiling with warnings (gcc -Wall -Wextra) catches many issues; run with AddressSanitizer or valgrind to detect overruns. If scanf must be used, always include a width (for example %99s) to prevent overflow. For reference on safe input and string functions see the standard documentation for fgets and strlen (fgets, strlen) and read about undefined behavior from invalid memory writes ().

Recommended Answers

All 2 Replies

Which compiler you are using ? Are you not getting any error because you are not returning any int value from function "len" as define in declaration. It is giving correct result on VS2010.

You are overwriting your arrays, when you declare a variable char str1[]="" since you have not specified an array length but you have specified an array initialiser the compiler allocates exactly the right array size to fit the initialiser into it. In your case an array size of 1 or the equivalent of char str1[1]="" .

When you scanf your strings it copies the input string into the array and adds a zero terminator but that certainly goes outside the size of your allocated arrays.

In both cases that means the second scanf overwrites the input data of the first, in the second program this is evidenced by the length of the string being wrong.

Writing outside the bounds of an array is undefined behaviour, which is bad, and should be avoided.

Something like char str1[100]="",str2[100]=""; is a simplistic way to solve your problem (but don't type more than 99 characters).

sundip is also correct about failing to return a value from len.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.