strstr == NULL doesn't work,

Francisco Silva
#include <stdio.h>
#include <string.h>
#define N 5

char username[N+3][20]={"ana","sofia","maria","isabel","joao","hugo","francisco","pedro"};
char str[20];

read_username()
{
    printf("Insert your username: ");
    gets(str);
}

void searchusername(int n)
{
    int i;
    for(i=0;i<=n;i++)
    {
        if(strstr(username[i], str) != NULL)
            printf("username exists")
    }
}

int main()
{
    read_username();
    searchusername(8);
} 

I have the code to check if username exists, but i can´t seem to turn it around so i only get the printf when username doesn't exist, any other way without using NULL is also okay, ty.

Remy Lebeau

One issue is that you are not avoiding a buffer overflow with gets(). I have to assume for this example that you are typing in short usernames that do not exceed 19 characters. Anything longer is going to cause problems if you don't account for it.

More importantly, you are not comparing the usernames correctly. You should not be using strstr() for that purpose. It searches for a substring inside of another string, it does not compare strings. For example, if you typed in ia, strstr() would match with both sofia and maria, both of which are wrong results for a username lookup. Use strcmp() for comparisons.

Try something more like this instead:

#include <stdio.h>
#include <string.h>
#define N 8

char* username[N] = {"ana", "sofia", "maria", "isabel", "joao", "hugo", "francisco", "pedro"};
char str[20] = {0};

void read_username()
{
    printf("Insert your username: ");
    if (fgets(str, 20, stdin))
    {
        int len = strlen(str);
        if ((len > 0) && (str[len-1] == '\n'))
            str[len-1] = '\0';
    } 
}

void searchusername()
{
    for(int i = 0; i < N ; i++)
    {
        if (strcmp(username[i], str) == 0)
        {
            printf("username exists");
            return;
        }
    }
    printf("username does not exist");
}

int main()
{
    read_username();
    searchusername();
} 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

If statement to check if array is null or empty, doesn't work

MySQL Set 0 if null (SUM CASE) doesn't work

MySQL setting a column to null doesn't work

Why doesn't list.get(0).equals(null) work?

Why Doesn't Setting Jackson to Accept Empty Lists as Null Work?

Eclipse null check doesn't work with functions

Mysql concat doesn't work if a column is null, what is wrong with the query?

Mockito.when().thenReturn() doesn't work or returns null

Android M - GoogleAccountCredential setSelectedAccount doesn't work - name must not be null

Excel vba check if number is null: doesn't work as expected

Why doesn't my BufferedReader that reads while readLine!=null work?

Why doesn't below code work with (null,null)?

Freemarker - <#if ??> doesn't seem to work in identifying null objects

sequelize.js belongsTo not null doesn't work

Room (SQLite) WHERE clause with null arguments doesn't work

JPA specification doesn't work when relationship is null

Dart null safety doesn't work with class fields

Video.attachCamera(null) doesn't work

How destroy SelectionKey attachment? attach(null) doesn't work

getJSONArray doesn´t work optJSONArray return null

strstr() don't work in PHP

Column.ForeignKeyAction.SET_NULL doesn't work in ActiveAndroid

Checking NULL Pointer In C Doesn't Work

MySQL insert null doesn't work after server update

Detecting if object is null in Javascript - why doesn't this work?

React Context API doesn't work when passing null

null check of function pointer doesn't work

Is there a parameter to return the string strstr if it doesn't find it?

Upsert stored procedure doesn't work when target values are null