Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C Programming Code Examples

C > Strings Code Examples

C program to remove first occurrence of a word from string

/* C program to remove first occurrence of a word from string Write a C program to input any string from user and remove first occurrence of a given word from string. Write a function to remove first occurrence of a word from the string. How to remove first occurrence of a word from the string in C programming. Logic to remove first occurrence of a word from given string. Logic to remove first occurrence of a word Input string from user, store it in some variable say str. Input word to be searched from user, store it in some other variable say word. Run a loop from start of the string str to end. Inside loop, for each character in word match rest of characters with str. If all characters in word matched to str, then proceed to next step. If a word match is found then, shift all characters from current match position to left. */ #include <stdio.h> #include <string.h> #define maxsize 100 // Maximum string size /** Function declaration */ void removeFirst(char * str, const char * toRemove); int main() { char str[maxsize]; char toRemove[maxsize]; /* Input string and word to be removed from user */ printf("Enter any string: "); gets(str); printf("Enter string to be removed: "); gets(toRemove); removeFirst(str, toRemove); printf("\nString after removing '%s': \n%s", toRemove, str); return 0; } /* Remove first occurrence of a word from string */ void removeFirst(char * str, const char * toRemove) { int x, j; int len, removeLen; int found = 0; len = strlen(str); removeLen = strlen(toRemove); for(x=0; x<len; x++) { found = 1; for(j=0; j<removeLen; j++) { if(str[x+j] != toRemove[j]) { found = 0; break; } } /* If word has been found then remove it by shifting characters */ if(found == 1) { for(j=x; j<=len-removeLen; j++) { str[j] = str[j + removeLen]; } // Terminate from loop so only first occurrence is removed break; } } }

The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.

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.

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).

#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.

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,

Get string length. Returns the length of the C string str. The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

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.

The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.


Program 'takes a number' as input and finds a Greater number of the entered number using same digits. Take a number as input. Reverse the number and store it in the array. Using for



C program remove all 'occurrences' of a given word in string using loop. Logic to remove all occurrences of a word in a string. Input string from user, store it in some variable say string.


Prints the Fibonacci of a given number using recursion. In fibonacci series, each number is the sum of the two preceding numbers. 0, 1, 1, 2, 3, 5, 8, ... The following program returns

Dynamic array of structures. Add ONE element to the array. Allocate memory for one 'struct node'. Copy the data into the new element (structure). Print the array. Free all...



C language program take the number of days as input. For the number of years, divide the input by 365 and obtain its quotient. For the number of weeks, divide the input by 365 and