answersLogoWhite

0


Best Answer

#include

#include

void main( )

{

char st[20],sub[10],temp[10];

int pos, i, j;

clrscr( );

printf("Enter the main string:");

gets(st);

printf("\nEnter the substring to insert:");

gets(sub);

printf("Enter the index position:");

scanf("%d",&pos);

if(pos<=strlen(st)) {

for(i=0;i<=strlen(st);i++) {

if(i==pos)

{

for(j=0;st[i]!='\0';j++) // to store the 'st' to 'temp' from given position.

{

temp[j]=st[i];

i++;

}

temp[j]='\0';

i=pos;

for(j=0;sub[j]!='\0';j++) // to insert a sub-str to main string.

{

st[i]=sub[j];

i++;

}

for(j=0;temp[j]!='\0';j++) // Lastly to insert the 'temp' to 'st' after sub-str.

{

st[i]=temp[j];

i++;

}

st[i]='\0';

}}

printf("\nAfter adding the sub-string: %s",st);

}

else

printf("\nSorry, it is not possible to insert a substring in that position.");

getch();

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a C program to insert a sub-string into given main string from a given position?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

The substring method returns?

"[Substr] returns a string object with its contents initialized to a substring of the current object."


How do you use sub string in java?

Substring method creates smaller string form bigger string , every time a new string is created but original character array buffer is used. so even if original string is 1G in size and substring is just 1K memory held by substring is 1G because of backed array. this can cause memory leak and prevent original string from garbage collection even if there is no direct reference.


What is substr function does in c?

it will give the substring of length(as per the user) from the actual string,and the starting length from where it has to copy the substring from actual should be given by the user.


How do you show the location of an occurrence of the letter e in a string using javascript?

The location of a substring within a string in JavaScript can be found using the indexOf() method of the string type. That method will return the placement of a string as a numerical index, starting from 0. The method takes two arguments. The first is the substring you're looking for. In this case "e." The second argument is optional, and it's the "start" argument. If you define it, you define the index from which JavaScript will start looking for the substring. By default, this index is 0 (zero.) If you want to find all of the occurrences of a string using this method, you have to write a loop. To see a working example of that, check out the JSFiddle in the related links. I've also attached a good lesson on JavaScript strings in PDF.


How do you use indexOf in java?

indexOf is a method of the String class. Since the indexOf method is overloaded, I will be using the indexOf(String str) version in this example. According to the API Documentation, this method "Returns the index within this string of the first occurrence of the specified substring." So, if you wanted to find the position of the letter 'v' in the String 'Java' and print it out, you would do this: String str = "Java"; int i = str.indexOf("v"); System.out.println(i); If the character you passed in the indexOf method does not exist in the String, indexOf would return a -1 (negative one).

Related questions

What is th function of the charindex?

The CHARINDEX function in SQL is used to find the position of a specific character or substring within a string. It returns the starting position of the substring or character within the given string.


Do I need to write a program to find a substring in a given string in c plus plus?

No.


What is substr in oracle?

Substring function, the same as almost any other language. You define the start position and length of the string you want returned from the main string parameter.


The substring method returns?

"[Substr] returns a string object with its contents initialized to a substring of the current object."


Write a Java Program to find occurences of given word in a text?

Check the documentation of the String class, for a method that searches for a substring.


How do you use sub string in java?

Substring method creates smaller string form bigger string , every time a new string is created but original character array buffer is used. so even if original string is 1G in size and substring is just 1K memory held by substring is 1G because of backed array. this can cause memory leak and prevent original string from garbage collection even if there is no direct reference.


How would you implement a substr function that extracts a sub string from a given string?

substr(string, position [, count]) It extract substring starting from start and going for count characters. If count is not specified, the string is clipped from the start till the end


What is substr function does in c?

it will give the substring of length(as per the user) from the actual string,and the starting length from where it has to copy the substring from actual should be given by the user.


Python Substring: What is a Substring in Python?

One of the most used types in the Python language is the string. Characters can be figuratively enclosed in quotation marks to create strings. Python treats single quotes the same way it treats double-quotes. In Python, creating strings is as simple as assigning a value to a variable. For Example: Variable1 = &quot;Hello world&quot; Variable2 = &quot;Welcome to the world of Python.&quot; What is a Substring? Just picture what a car manufacturer does to quickly and effectively determine the last five digits of a chassis number. The idea of Substring conceals the answer to this puzzle. According to proper language analysis and computer science, a substring is a sequence of characters included within a string. In other words, a substring can be defined as a portion of a string that is created using one of the methods listed by the Python string, which also determines the substring index and other details. A component or subset of a string is another method to define a substring. The substring process includes any changes made to text data that make up a string. For example: &quot;This work is excellent. We must pursue it. It is a particular kind of string, and &quot;We must pursue it&quot; is a particular kind of Substring. Slicing is a Python function that can be used to extract a substring. Programmers frequently desire to divide the data into separate pieces for a particular reason. For instance, if a developer needs to use only the user's first name and has data that includes the user's whole name, the developer will need to divide the data into two pieces, such as the user's forename and surname. The challenge that arises now is how a Python programmer will carry out this task. The correct response is that a developer must engage in &quot;string slicing&quot; to complete this task. String slicing is a Python technique for extracting a specific portion of a string, which is then utilized as a &quot;substring.&quot; How is a Substring created from a String that is provided? Python supports several methods for creating a substring from a string. But the slicing process is one of the most used methods in Python for creating a substring from a string. Slicing in Python Based on their position in a string, a software developer can access the characters within it at any time. Characters are assembled into strings. The term for this is indexing. The indexing method in Python returns a one-character string from the supplied offset or location. Now, slicing is the technique used to do this action when a portion of a string, rather than simply a single character, is required. What is String Slicing In Python? Slices, which return a complete necessary chunk in a single step rather than a single item, might be considered a generalized indexing type. Slicing allows for the execution of various tasks, including the extraction of data columns, the removal of leading and trailing characters, and many more. Slicing relies on a very basic idea. A new string object containing the section indicated by the offset pair is returned by Python when a string is indexed using a pair of offsets separated by a colon (:). The right offset's upper bound is non-inclusive in the offset pair, whereas the left offset's lower bound is included. Therefore, the left and right boundaries will default to values of 0 and the length of the string you are slicing, respectively, if neither of the offsets is given. Let's get into the specifics to comprehend the Slicing operator's syntax. Syntax of Slicing Operator As we have previously read earlier, the slicing operator is thought to be one of the best ways to create a substring. Let's examine the slicing operator's syntax: String(steps, startIndex, endIndex) where, • startIndex: This is the Substring's initial index. The character is a part of the Substring at this index. StartIndex is presumed to be equal to 0 if it is not set. • endIndex: This is Substring's final index. The character is absent from the Substring at this index. If the endIndex value is left blank, it is automatically considered equal to the length of the entire string. • Step: After the first character is extracted from the string, it is known how many characters to proceed with. The default setting is 1. Interested in becoming a data scientist? To learn more about Substring and other Python methods, enroll in a data science course in Mumbai, please visit Learnbay.co


What is the purpose of a substring?

A string that is a part of bigger string..


What is the meaning behind a javascript split?

The javascript split method, split(), will alter a string to make it into an array of substrings. This is useful if you would like to store each substring from the original string seperately.


How do you show the location of an occurrence of the letter e in a string using javascript?

The location of a substring within a string in JavaScript can be found using the indexOf() method of the string type. That method will return the placement of a string as a numerical index, starting from 0. The method takes two arguments. The first is the substring you're looking for. In this case "e." The second argument is optional, and it's the "start" argument. If you define it, you define the index from which JavaScript will start looking for the substring. By default, this index is 0 (zero.) If you want to find all of the occurrences of a string using this method, you have to write a loop. To see a working example of that, check out the JSFiddle in the related links. I've also attached a good lesson on JavaScript strings in PDF.