C Programming Code Examples
C > Strings Code Examples
program to remove all repeated characters in a string
/* program to remove all repeated characters in a string
Write a C program to remove all repeated characters in a string using loops. How to remove all duplicate characters from a string using for loop in C programming. Program to find and remove all duplicate characters in a string. Logic to remove all repeated character from string in C program.
Logic to remove repeated characters from string
Input string from user, store it in some variable say str.
Run a loop from start to end character of the given string str.
For each character ch in the string, remove all next occurrences of ch. */
#include <stdio.h>
#define maxsize 100 // Maximum string size
/* Function declarations */
void removeDuplicates(char * str);
void removeAll(char * str, const char toRemove, int index);
int main()
{
char str[maxsize];
/* Input string from user */
printf("Enter any string: ");
gets(str);
printf("String before removing duplicates: %s\n", str);
removeDuplicates(str);
printf("String after removing duplicates: %s\n", str);
return 0;
}
/* Remove all duplicate characters from the given string */
void removeDuplicates(char * str)
{
int x = 0;
while(str[x] != '\0')
{
/* Remove all duplicate of character string[x] */
removeAll(str, str[x], x + 1);
x++;
}
}
/* Remove all occurrences of a given character from string. */
void removeAll(char * str, const char toRemove, int index)
{
int x;
while(str[index] != '\0')
{
/* If duplicate character is found */
if(str[index] == toRemove)
{
/* Shift all characters from current position to one place left */
x = index;
while(str[x] != '\0')
{
str[x] = str[x + 1];
x++;
}
}
index++;
}
}
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance. The while loop evaluates the test expression inside the parentheses (). If test expression is true, statements inside the body of while loop are executed. Then, test expression is evaluated again. The process goes on until test expression is evaluated to false. If test expression is false, the loop terminates.
Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,
The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.
C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
In the C Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.
Get string from stdin. Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached. The newline character, if found, is not copied into str. A terminating null character is automatically appended after the characters copied to str. Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows).
C Program code to 'Count Frequency' of each character in a string Using Loop. There are so many algorithms to Count Frequency of each character. Here I'm explaining the easiest one
Zero on success or nonzero on failure. Seek from start of file. Seek from current location, seek from end of file, find proper structure, read the data into memory in c programming
This C Program to Calculates the value of nCr. The algorithm used in this program is nCr = n! /((n-r)!r!) and We need to Find all the Possible combination of the value n&r. A combination
C program to compare two strings using loop character by character. Logic to compare two strings. Input two strings from user and Store it in some variable say str1 and str2. Compare
Upside down triangle. A triangle with all sides equal is called "Equilateral Triangle". We shall now see how to print stars char, in equilateral triangle shape, but 'upside-down'. Equilateral
We will use the above logic inside if to check number for Negative, Positive or Zero. Step by step Descriptive Logic to check negative, positive or zero. Input a number from user in