0% found this document useful (0 votes)
7 views6 pages

C Programs for String Manipulation and Number Finding

The document contains four C programs: one for inserting a substring into a main string at a specified position, another for deleting a specified number of characters from a given position in a string, a third for checking if a string is a palindrome, and the last for finding the largest and smallest numbers in a list of integers. Each program includes user input prompts, string manipulation techniques, and output statements. The code snippets demonstrate basic C programming concepts such as loops, conditionals, and string handling.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

C Programs for String Manipulation and Number Finding

The document contains four C programs: one for inserting a substring into a main string at a specified position, another for deleting a specified number of characters from a given position in a string, a third for checking if a string is a palindrome, and the last for finding the largest and smallest numbers in a list of integers. Each program includes user input prompts, string manipulation techniques, and output statements. The code snippets demonstrate basic C programming concepts such as loops, conditionals, and string handling.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Write a C program To insert a sub-string into a given main string from a given

position.
#include <stdio.h>

#include <string.h>

int main() {

char mainStr[100], subStr[50], result[150];

int pos, i, j;

// Input main string

printf("Enter the main string: ");

gets(mainStr);

// Input sub-string

printf("Enter the sub-string to insert: ");

gets(subStr);

// Input position

printf("Enter the position to insert the sub-string: ");

scanf("%d", &pos);

// Copy characters from mainStr before position

for (i = 0; i < pos; i++) {

result[i] = mainStr[i];

// Insert the sub-string

for (j = 0; subStr[j] != '\0'; j++) {


result[i++] = subStr[j];

// Copy remaining part of mainStr

for (j = pos; mainStr[j] != '\0'; j++) {

result[i++] = mainStr[j];

// Null terminate the result string

result[i] = '\0';

printf("Resultant string after insertion: %s\n", result);

return 0;

Write a C program To delete n Characters from a given position in a given


string.

#include <stdio.h>

#include <string.h>

int main() {

char str[100];

int pos, n, i, len;

// Input the string

printf("Enter the main string: ");


gets(str);

// Input position and number of characters to delete

printf("Enter the position to delete from: ");

scanf("%d", &pos);

printf("Enter number of characters to delete: ");

scanf("%d", &n);

len = strlen(str);

// Shift characters to the left starting from (pos + n)

for (i = pos; i < len - n; i++) {

str[i] = str[i + n];

// Null terminate the new string

str[i] = '\0';

// Display result

printf("String after deletion: %s\n", str);

return 0;

Write a C program to determine if the given string is a palindrome or not


#include <stdio.h>

#include <string.h>

int main() {
char str[100], rev[100];

int len, i;

printf("Enter a string: ");

gets(str); // Note: gets() is unsafe, but used for simplicity.

len = strlen(str);

// Reverse the string

for (i = 0; i < len; i++) {

rev[i] = str[len - i - 1];

rev[i] = '\0'; // Null terminate the reversed string

// Compare original and reversed string

if (strcmp(str, rev) == 0) {

printf("The string is a palindrome.\n");

} else {

printf("The string is not a palindrome.\n");

return 0;

Write a C program to find both the largest and smallest numbers in a list of
integers.
#include <stdio.h>

int main() {
int n, i, num;

int largest, smallest;

printf("Enter the number of elements: ");

scanf("%d", &n);

if (n <= 0) {

printf("Invalid input! Number of elements must be greater than 0.\n");

return 0;

printf("Enter number 1: ");

scanf("%d", &num);

largest = num;

smallest = num;

for (i = 2; i <= n; i++) {

printf("Enter number %d: ", i);

scanf("%d", &num);

// Using if-else ladder to compare

if (num > largest) {

largest = num;

else if (num < smallest) {

smallest = num;

}
else {

// No change if number is between largest and smallest

printf("\nLargest number = %d\n", largest);

printf("Smallest number = %d\n", smallest);

return 0;

You might also like