-2

My task is: Write a program that calculates the length of a string without using the library

This is my answer, but there is a problem with execution. The length doesnt show properly ! the execution shows length as 107 or 127 for any string I insert.

#include <stdio.h>
#include <stdlib.h>

int main()
{
   //Declaration of variables :
   char ch[50+1];
   int length, i;

   //data :
   printf("ch : ");
   scanf("%s", &ch);
   printf("\n");

   //Search length of string :
   i = 0;
   do
   {
       if(ch[i] == '\0')
       {
           length = i;
       }
       else
       {
           i++;
       }
   }
   while(ch[i] != '\0');

   //Result "
   printf("length pf %s is : %d \n", ch, length);

   return 0;
} ```
  • Step through your program. The line `length = i;` never gets executed. (I think the compiler should have warned you about this, so check your compiler flags.) – Jongware Jun 08 '20 at 17:06
  • no it didn't. the program executes but shows a length of 107 – Bacim Oueslati Jun 08 '20 at 17:08
  • "*for any string i insert !*" - Note that you can only insert a string up to 50 characters. Else you write beyond the bounds of the array which invokes undefined behavior. Use a length modifier to ensure that this won´t happen like `scanf("%50s", ch);` – RobertS supports Monica Cellio Jun 08 '20 at 17:10
  • That is not true. It shows an *uninitialized* value; you yourself say that it *sometimes* returns one or another. Don't believe me? Test it by adding `length = 2020;` **before** the loop. Now it will always return `2020`. – Jongware Jun 08 '20 at 17:10
  • okay then why didnt the do while loop get the length ? where is the bug please ? – Bacim Oueslati Jun 08 '20 at 17:12
  • BTW: `scanf("%s", &ch);` -> `scanf("%s", ch);` -> format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[51]' [-Werror=format=] – RobertS supports Monica Cellio Jun 08 '20 at 17:15

1 Answers1

1

There is a problem with the algorithm of the do-while loop.

The counter i increments short before the condition check.

If '\0' is found in the next array element (Note, that i is incremented) the loop breaks immediately and won´t be able to set length to i at the next iteration (because there is no next iteration).

Since length is not initialized, the program has undefined behavior.

Change:

do
{
   if (ch[i] == '\0')
   {
       length = i;
   }
   else
   {
       i++;
   }
}
while (ch[i] != '\0');

to

while (ch[i] != '\0') i++;

length = i;

or even simpler:

while (ch[i] != '\0') length++;

and omit the counter i, but you need to initialize length by 0 then.


Side Notes:

  1. Change scanf("%s", &ch); to scanf("%s", ch);. - ch decays to a pointer to its first element.

  2. Use a length modifier at scanf() -> scanf("%50s", ch); to ensure that no buffer overflow occurs when the user inputs a string longer than 50 characters.

  3. Always check the return value of scanf() if an error occurred at consuming input.

  4. Never ignore at the compiler warnings. For scanf("%50s", ch); the compiler should have raised a warning.