Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Strings. No Comments on C program to perform the different operations on strings

In this article we will learn to implement a C program to perform the different operations on strings using functions. A C program is provided below to perform various operations on strings.

 

/*
 * C program to perform the following operations on strings using functions:
 * i. To insert a sub string in to given main string from a given position.
 * ii. To delete n characters from a given position in a given string.
 * iii. To replace a character from the string either from beginning or ending or at a specified location.
 * Author: P.S.SuryaTeja
*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

void insertsubstr(char *str)
{
	char substr[10], newstr[30];
	int pos, strlength;
	int i, j, k;
	strlength = strlen(str);
	printf("\nEnter a substring: ");
	gets(substr);
	printf("\nEnter the index at which the substring should be inserted: ");
	scanf("%d", &pos);
	for(i = 0; i < pos; i++)
	{
		newstr[i] = *str;
		str++;
	}
	for(j = i, k = 0; k < strlen(substr); j++, k++)
		newstr[j] = substr[k];
	for(i = j, k = 0; k < strlength; i++, k++)
	{
		newstr[i] = *str;
		str++;
	}
	newstr[i] = '\0';
	printf("\nNew string after insertion is: ");
	puts(newstr);
}

void deletechars(char *str)
{
	char newstr[20];
	int nchars, pos;
	int i, j, strlength;
	strlength = strlen(str);
	printf("\nEnter no. of characters to be deleted: ");
	scanf("%d", &nchars);
	printf("\nEnter the index from which the characters should be deleted: ");
	scanf("%d", &pos);
	for(i = 0; i < pos; i++)
	{
		newstr[i] = *str;
		str++;
	}
	for(j = 0; j < nchars; j++)
		str++;
	for(j = 0; i < strlength - nchars ; i++, j++)
	{
		newstr[i] = *str;
		str++;
	}
	newstr[i] = '\0';
	printf("\nNew string after deletion is: ");
	puts(newstr);
}

void replacechar(char *str)
{
	int choice, pos, i, strlength;
	char newchar, fake;
	char *tempstr;
	tempstr = str;
	strlength = strlen(str);
	printf("\n1.Replace a character at the beginning.");
	printf("\n2.Replace a character at the end.");
	printf("\n3.Replace a character at any position.");
	printf("\nEnter your choice: ");
	scanf("%d", &choice);
	switch(choice)
	{
		case 1:
			printf("Enter the replacement character: ");
			scanf("%c%c", &fake, &newchar);
			*str = newchar;
			printf("\nNew string after replacement is: ");
			puts(str);
			break;
		case 2:
			printf("Enter the replacement character: ");
			scanf("%c%c", &fake, &newchar);
			for(i = 0; i < strlength - 1; i++)
				str++;
			*str = newchar;
			printf("\nNew string after replacement is: ");
			puts(tempstr);
			break;
		case 3:
			printf("\nEnter the index at which character will be replaced: ");
			scanf("%d", &pos);
			printf("\nEnter the replacement character: ");
			scanf("%c%c", &fake, &newchar);
			for(i = 0; i < pos; i++)
				str++;
			*str = newchar;
			printf("\nNew string after replacement is: ");
			puts(tempstr);
			break;
		default:
			printf("\nInvalid choice");
	}
}

int main(int argc, char **argv)
{
	char str[20];
	printf("Enter a string: ");
	gets(str);
	printf("\n***Inserting substring***");
	printf("\n");
	insertsubstr(str);
	printf("\n***Deleting Characters***");
	printf("\n");
	deletechars(str);
	printf("\n***Replacing a character***");
	printf("\n");
	replacechar(str);
    getch();
    return 0;
}

 

Input and output for the above program is as follows:

Enter a string: I am a good boy

***Inserting substring***

Enter a substring: bad

Enter the index at which the substring should be inserted: 6

New string after insertion is: I am abad good boy

***Deleting Characters***

Enter no. of characters to be deleted: 3

Enter the index from which the characters should be deleted: 6

New string after deletion is: I am aod boy

***Replacing a character***

1.Replace a character at the beginning.
2.Replace a character at the end.
3.Replace a character at any position.
Enter your choice: 1
Enter the replacement character: U

New string after replacement is: U am a good boy

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

Your email address will not be published. Required fields are marked *