Click here to Skip to main content
15,886,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried it without using function and the code gives me correct output but when imposing a function on the code the code is always giving me "not unique chars" as the output, I am thinking that pointer is being changed in while comparing the characters or may be other reason which I am not getting. Please help

What I have tried:

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

int uniquechar(char [] , int);

main()
{
    char c[100];
    int n,x;

    scanf("%s",c); //taking string input
    n = strlen(c);  //length of string

    x = uniquechar(c,n); //function for unique characters

    if(x == 1)
        printf("Not unique char");
    else
        printf("Unique chars");
}

int uniquechar(char b[] , int n)
{
    int i , j , flag = 0;

    for(i = 0;i < n;i++)
    {
        for(j = i+1;j < n;j++)
        {
            if(b[i] == b[j])
            {
                flag = 1;
                break;
            }
        }
    }
    if(flag = 1)
        return 1;
    else
        return 0;
}
Posted
Updated 19-Aug-17 20:50pm

This is the problem:
C++
if(flag = 1)

A single = means assignment, i.e. "I give a value to this variable". The compiler does not give an error, however, because an expression name = value returns value after setting the variable, so what you are actually doing here is if (1) { ... } and that's always true. You need the equality operator == instead.
 
Share this answer
 
Comments
Vishal Gupta 20-Aug-17 2:42am    
thanks, these silly mistakes I do so much in coding and always stuck, so can you give me a solution how can I overcome these kinds mistakes
Thomas Daniels 20-Aug-17 2:48am    
Learn to use a debugger. It's a tool that lets you run your code step-by-step and check what the current values of variables are and such. If the debugger step would be at this if statement, you'd look at it and probably notice what you did wrong. Everyone makes mistakes, and a debugger helps you find them :-)
This code:
C++
if(flag = 1)
    return 1;
else
    return 0;

can be simplified to
C++
return flag;

and it also solve the problem as explained in Solution 1. Learn debugger, this tool helps to locate points of failure in code.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900