C program to replace the sub string with another string.

C Strings:
Write a C program to replace the sub string in a string with another string.
In this program, We replace string with another string, if the entered string is the sub string of main string, otherwise function replace_str returns the original string as it is. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <string.h>

char *replace_str(char *str, char *orig, char *rep)
{
static char buffer[4096];
char *p;

if(!(p = strstr(str, orig)))
return str;

strncpy(buffer, str, p-str);
buffer[p-str] = '';

sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));

return buffer;
}

int main(void)
{
char str[100],str1[50],str2[50];
printf("Enter a one line string..n");
gets(str);
printf("Enter the sub string to be replaced..n");
gets(str1);
printf("Enter the replacing string....n");
gets(str2);
puts(replace_str(str, str1, str2));

return 0;
}
Read more Similar C Programs
Searching in C

C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to demonstrate the strstr function.

C Program to demonstrate the strstr function.
strstr function finds the substring in a string. strstr() returns pointer of the first occurrence of the string other wise it returns the null pointer is returned.
In this program strstr returns a pointer into ‘string’ if ‘test’ is found, if not found, NULL is returned. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
#include <string.h>

main()
{
char string[]="string to search";
char test[]="sear";
if (strstr(string, test)) puts("String found");

}
Read more c programs 
C Basic
C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to count the number of occurrences of particular word in a string

C Program that counts the particular word in the given string. For example in the string ” the string is the set of charterers” , Number of occurrences of “the” is:2. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>

#include<string.h>

main()

{

 int strln,wordln,i,j,k,flag,count=0;

 char str[200],word[20];

 printf("Enter line of text:n");

 gets(str);

 printf("Enter the word to count:n");

 scanf("%s",word);

 strln=strlen(str);

 wordln=strlen(word);

 for(i=0;i<strln;i++)

 {

  if(str[i]==word[0]&&((str[i-1]==' '||i==0)&&(str[i+wordln]==' '||str[i+wordln]=='')))

  {

   for(flag=0,k=i+1,j=1;j<wordln;j++,k++)

   {

    if(str[k]==word[j])

    {

     flag++;

    }

   }

   if(flag==wordln-1)

   {

    count++;

   }

  }

 }

 printf("Number of occurence of '%s' = %dn",word,count);

}

Read more Similar C Programs
Searching in C

C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

Like to get updates right inside your feed reader? Grab our feed! 

(c) www.c-program-example.com

C program to insert a sub-string in to given main string from a given position.

C Strings:
C Program to insert a sub-string in to given main string from a given position. In this program we read two strings and replace the second string in the given position of the first string.Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;
int t=0;
int x,g,s,n,o;
clrscr();

puts("Enter First String:");
gets(a);
puts("Enter Second String:");
gets(b);
printf("Enter the position where the item has to be inserted: ");
scanf("%d",&p);
r = strlen(a);
n = strlen(b);
i=0;

// Copying the input string into another array
while(i <= r)
{
c[i]=a[i];
i++;
}
s = n+r;
o = p+n;

// Adding the sub-string
for(i=p;i<s;i++)
{
x = c[i];
if(t<n)
{
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}

printf("%s", a);
getch();
}
Read more Similar C Programs
Learn C Programming

C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to accept a string and a substring and check if the substring is present in the given string

C Strings:
Write a C program to accept a string and a substring and check if the substring is present in the given string. Program search’s given substring is in the string or not, and prints the appropriate message. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/




#include<stdio.h>
#include<conio.h>

void main()
{
char str[80],search[10];
int count1=0,count2=0,i,j,flag;


clrscr();

puts("Enter a string:");
gets(str);

puts("Enter search substring:");
gets(search);

while (str[count1]!='')
count1++;

while (search[count2]!='')
count2++;

for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
{
flag=1;
if (str[j]!=search[j-i])
{
flag=0;
break;
}
}
if (flag==1)
break;
}
if (flag==1)
puts("SEARCH SUCCESSFUL!");
else
puts("SEARCH UNSUCCESSFUL!");
getch();
}
Read more Similar C Programs
Searching in C

C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com