Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

C Program to Find the Second Last Character of a String

Second Last Character of String in C (scanf) - No Newline Handling

✅ C Program to Find the Second Last Character of a String (Clean scanf Version)

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

int main() {
    char str[100];

    printf("Enter a string: ");
    scanf("%[^\n]", str);  // Reads until newline, no need to handle '\n'

    int len = strlen(str);

    if (len < 2) {
        printf("String is too short to have a second last character.\n");
    } else {
        printf("Second last character: %c\n", str[len - 2]);
    }

    return 0;
}
  

πŸ“˜ Explanation:

This program reads a string from the user using scanf("%[^\n]"), which captures input until the newline character but does not include it in the string.

  • It directly uses strlen() to find the length of the input.
  • If the string has fewer than 2 characters, a warning is displayed.
  • Otherwise, str[len - 2] gives the second last character.
  • No need for additional newline removal logic in this version.

🧾 Sample Output:

Enter a string: Hello World
Second last character: l

Enter a string: H
String is too short to have a second last character.
  

πŸ”‘ Keywords:

scanf string C, second last character, strlen example, no newline handling, C string logic, interview question, clean code C

πŸ“Œ Hashtags:

#CProgramming #StringIndexing #strlen #scanfInput #InterviewCode #BeginnerFriendly

πŸ” Search Description:

Clean and efficient C program to find the second last character in a string using scanf without newline handling. Simple logic using strlen. Great for C beginners.

Comments

Popular Posts

πŸŒ™