Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

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

πŸŒ™