Home » C solved programs » C string programs

C program to copy one string to another and count copied characters

In this C program, we are going to learn how to copy one string to another without using any library function? Here, we will also get the total number of copied characters and printing them.
Submitted by IncludeHelp, on March 14, 2018

Given a string and we have to copy it in another string, also find and print the total number of copied characters using C program.

Example:

    Input:
    String 1 (Input string): "This is a test string"

    Now, we will copy string 1 to the string 2
    Output:
    String 1: "This is a test string"
    String 2: "This is a test string"
    Total numbers of copied characters are: 21

Logic:

Copy character by character from string 1 to the string 2 and use a loop counter which is initialized by 0, after copying each character, increase the loop counter. At the end, assign NULL (‘\0’) to the target (string 2) and loop counter will be the total number of copied characters.

In the given program, we have created a user defined function to copy one string to another string and returning the total number of copied characters.

Function prototype: int copy_string(char *target, char *source)

Where,

  • Parameters:
    • char *target - pointer to target string in which we will copy the string
    • char *source - pointer to source string whose value we want to copy
  • return type: - int : total number of copied characters

Program to copy one string to another string and find total number of copied character in C



#include <stdio.h>

//function to copy the string
int copy_string(char *target, char *source)
{
	//variable tp store length, it will also work
	//as loop counter
	int len=0;
	
	//loop to run from 0 (len=0) until NULL ('\0')
	//not found
	while(source[len] != '\0')
	{
		target[len] = source [len];
		len++;
	}
	//assign NULL to the target string
	target[len] = '\0';
	
	//return 'len'- that is number of copied characters
	return len;
}

//main code
int main()
{
	//declare and assign string1 to be copied
	char str1[]="This is a test string";
	//declare string 2 as target string
	char str2[30];
	//variable to store number of copied characters
	//that will be returned by the copy_string()
	int count;
	
	//calling the function
	count = copy_string(str2,str1);
	
	//printing values
	printf("Source string (str1): %s\n",str1);
	printf("Target string (str2): %s\n",str2);
	printf("Copied characters are: %d\n",count);
	
	return 0;	
}

Output

Source string (str1): This is a test string
Target string (str2): This is a test string
Copied characters are: 21	





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.