Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What's wrong with my code? Please help (C)

+7 votes
asked Dec 24, 2022 by Eidnoxon (1,990 points)
Hi! I'm trying to learn the C programming language, but while I was coding, I got an error. Can somebody help? https://onlinegdb.com/GPhYl7hHhK

4 Answers

+2 votes
answered Dec 24, 2022 by Nitesh Shrestha (180 points)
#include <stdio.h>
#include <string.h>//cause we are woking with string variable.
int main(){
    char name[100];
    printf("Hello, User! What name should I call you?");
    scanf("%s", name);
    
    if (strlen(name) <= 2) //strlen is tag to count the length of a name(string).
    {
        printf("Nope, Your name has to be atleast 3 characters!");
    } else {
        printf("Oh, Hi, %s!", name);
    }
}
0 votes
answered Dec 24, 2022 by Samikshya B. Chhetri (220 points)
you need to first measure the lenth of string using strlen() function and then use if else condition
0 votes
answered Dec 25, 2022 by Sagar Kumar (140 points)
#include <stdio.h>
#include <string.h>
int main(){
    char name[100];
    printf("Hello, User! What name should I call you?\t");
    scanf("%s",name);
    int a = strlen(name);
    if (a <= 2) {
        printf("Nope, Your name has to be atleast 3 characters!");
    } else {
        printf("Oh, Hi, %s!", name);
    }
}
0 votes
answered Dec 27, 2022 by Soumya Khandelwal (180 points)
For calculating lenght of string ,you should use strlen function which is included in string.h library file.

#include<stdio.h>

#include<string.h>

int main()

{
    char name[100];
    int len;
    printf("Hello, User! What name should I call you?");
    scanf("%s", name);
    len=strlen(name);
    
    if (len <= 2) {
        printf("Nope, Your name has to be atleast 3 characters!");
    } else {
        printf("Oh, Hi, %s!", name);
    }
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...