Latest :

C Program : Non – Repeating Characters in A String | C Programs

C program to display all the non-repeating characters in a given string. To do so, we will require a string whose non-repeating characters are to be displayed as our input. The desired output here is, displaying every character that is non-repeating in the given input string.

So initially, we will read a input string (c) at runtime using gets() function. This predefined function in C language reads one line of string everytime and stores it in a string pointer. It reads the input until it reaches either end-of-fine or a new line.

char c[150];

printf(“Enter a string : “);

gets(c);

After getting the input, to display all the non-repeating characters, we iterate using looping statements from 0th index (i=0) to the last index i.e., until c[i] exists. We also maintain a count variable (t) which is initialize of zero for every iteration or for every character to be checked in the string.

Then, we iterate through the string from beginning (j=0) to end (c[j] is not null or exists) and check whether any character is equal to the character of the outer loop iteration i.e., if c[i]=c[j]. If we find a character satisfying that condition, we increment the count variable (t) by 1.

In the same loop we also check whether count variable (t) is greater than one.

If this condition satisfies for any character then it means that, the character is repeating. So, we break the inner loop there itself. In the outer loop, we again check whether the count variable (t) is equal to 1 or not.

If it is equal to 1 then, we display the character on the output console screen using printf() function and proceed with next iteration.

Else, we do not print anything on the console screen and proceed with the next iteration.

for(i=0;c[i];i++ ) {

t=0;

for(j=0;c[j];j++) {

if(c[i]==c[j])

t++;

if(t>1)

break;

}

if(t==1) {

printf(“%c “,c[i]);

}

}

When all the iterations are completed, we will be having all the non-repeating characters of the given input string displayed on the output console screen. This was the desired output that we have finally achieved.

Output:

Using Function

In the above methodology discussed, we have seen the logic behind displaying all the non-repeating characters in a string. But since the entire code is written within main method, there are few disadvantages. If the same logic is to be used elsewhere in the code later, we will have to rewrite it again.

Also, if some part of the code is to be found, we will have to search the entire main method for it. Hence, the code is neither reusable nor very readable.

But, this can be overcome by using functions. If we split the code based on its functionality or logic and put it in separate function blocks then, it becomes a lot convenient.

If the logic is needed elsewhere then, we can just make a function call and pass the required parameters.

Also, if a part of code is to be found, we can directly go and search in its corresponding function block. So now, the code becomes cleaner.

To implement the same for the given problem statement, we’ll first get our input string at runtime using gets() function in the main method.

We then make a function call to a user-defined function (NonRepeat) which consists of the same logic discussed earlier.

We pass our input string whose non-repeating characters are to be displayed as parameter to this function (NonRepeat).

This function then iterates and finally prints all the non-repeating characters on the console screen. This way, the main method has the i/o operations related statement while the separate user-defined function has the logic to solve the given problem statement.

x

Check Also

C Program To Check A Number Is Negative, Positive Or Zero | C Programs

C program to check whether a number is negative, positive or zero – In this ...