<<< Print a string using a loop | Logical Flow of Code (LFoC)

{

    char a[100];

    int i=0;


    gets(a);

    while(a[i]!='\0')

    {

            printf("%c", a[i]);

            i++;

    }

    return 0;

}



Input: My name is khan

Output: My name is khan

#AbdurRahimRatulAliKhan #CodingLogic #Looping #Programming #StringPrinting


This code is the program that takes a string as input and then prints it character by character using a loop. The program starts by declaring a character array 'a' with a size of 100 and an integer variable 'i' initialized to 0.

The input string is read using the 'gets' function, which may not be the safest way to read input due to potential buffer overflow issues. However, for the sake of simplicity, it's used in this code snippet.

Next, the program enters a while loop that iterates until it encounters the null terminator '\0' character, which marks the end of the string. Inside the loop, the program prints each character of the string using the 'printf' function with the format specifier '%c'.

Finally, the program returns 0 to indicate successful execution.

Given the input string "My name is khan," the output will be "My name is khan" since the program prints the characters of the string without any modification.


(Note: To improve the code, it's recommended to use a safer input function and also handle potential buffer overflow issues.)