Home » C PROGRAM » C PROGRAM TO PRINT COPY CONCATENATE STRING

C PROGRAM TO PRINT COPY CONCATENATE STRING

A string is a character array which stores characters as an Integer is stored in Integer array. Each string is terminated by a null denoted by “\0”. The null character “\0” tells the function where the string is ended.

C PROGRAM TO PRINT A STRING

A string can be printed by many methods. Each method is shown below. In the first program the length of string is know. so a while loop is used to print the string.

#include<stdio.h>

#include<conio.h>

void main()

{

            char string[] = “tiger”;

            int i;

            clrscr();

            printf(“The String is “);

            i = 0;

            while(i <= 5)

            {

                        printf(“%c”, string[i]);

                        i++;

            }

            getch();

}

C PROGRAM TO PRINT A STRING USING WHILE LOOP:

In this program the length of the string is not known. we have initialized the character array and it is compared with ‘\0’. Because we know that each string ends with a ‘\0’.

#include<stdio.h>

#include<conio.h>

void main()

{

            char string[] = “tiger”;

            int i;

            clrscr();

            printf(“The String is “);

            i = 0;

            while(string[i] != ‘\0’)

            {

                        printf(“%c”, string[i]);

                        i++;

            }

            getch();

}

C PROGRAM TO PRINT A STRING USING WHILE LOOP AND POINTER:

#include<stdio.h>

#include<conio.h>

void main()

{

            char string[] = “tiger”;

            char *s;

            clrscr();

            printf(“The String is “);

            s = string;

            while(*s != ‘\0’)

            {

                        printf(“%c”, *s);

                        *s++;

            }

            getch();

}

C PROGRAM TO PRINT A STRING USING FORMAT SPECIFICATION OR %S

#include<stdio.h>

#include<conio.h>

void main()

{

            char string[] = “tiger”;

            clrscr();

            printf(“The String is “);

            printf(“%s”, string);

            getch();

}

C PROGRAM TO PRINT A STRING USING PUTS FUNCTION:

#include<stdio.h>

#include<conio.h>

void main()

{

            char string[] = “tiger”;

            clrscr();

            printf(“The String is “);

            puts(string);

            getch();

}

OUTPUT OF THE PROGRAM:

The String is tiger

C PROGRAM TO FIND LENGTH OF A STRING

#include<stdio.h>

#include<conio.h>

// Declaration of Maximum size of the String Array

#define MAX 100

void main()

{

            int length, i;

            char string[MAX];

            clrscr();

            printf(“Enter the String or word.\n”);

            gets(string);

// gets() function can take multi-word input

// while scanf() can not

            printf(“The String is “);

            puts(string);

// puts() function is used for printing

// puts() is the counterpart of the gets()

            i = 0;

            length = 0;

            while(string[i] != ‘\0’)

            {

                        length++;

                        i++;

            }

            printf(“Length of the String is %d.”, length);

            getch();

}

C PROGRAM TO FIND LENGTH OF STRING BY STRLEN FUNCTION:

#include<stdio.h>

#include<conio.h>

#include<string.h>

// Declaration of Maximum size of the String Array

#define MAX 100

void main()

{

            int length;

            char string[MAX];

            clrscr();

            printf(“Enter the String or word.\n”);

            gets(string);

            printf(“The String is “);

            puts(string);

            length = strlen(string);

            printf(“Length of the String is %d.”, length);

            getch();

}

C PROGRAM TO FIND LENGTH OF A STRING USING FUNCTION:

#include<stdio.h>

#include<conio.h>

// Declaration of Maximum size of the String Array

#define MAX 100

// Declaration of String length function

int string_length(char *s);

void main()

{

            int length;

            char string[MAX];

            clrscr();

            printf(“Enter the String or word.\n”);

            gets(string);

            printf(“The String is “);

            puts(string);

// Calling String length function

            length = string_length(string);

            printf(“Length of the String is %d.”, length);

            getch();

}

// Definition of the String length function

int string_length(char *s)

{

            int len;

            len = 0;

            while(*s != ‘\0’)

            {

                        len++;

                        *s++;

            }

            return (len);

}

OUTPUT OF THE PROGRAM:

Enter the String or word.
Hello, world
The String is Hello, world
Length of the String is 12.

C program output to find length of string

C PROGRAM TO CONCATENATE TWO STRINGS

#include<stdio.h>

#include<conio.h>

#include<string.h>

// Declaration for Maximum size of the String

#define MAX 100

void main()

{

int i, j, length;

char string1[MAX], string2[MAX];

clrscr();

printf(“Enter First String.\n”);

// gets() function is used to take input

gets(string1);

printf(“First String is %s.\n”, string1);

printf(“Enter Second String.\n”);

gets(string2);

printf(“Second String is %s.\n”, string2);

// strlen() function is used to find length of a string

length = strlen(string1);

i = 0;

j = length;

// Code for Concatenating Strings

while(string2[i] != ‘\0’)

{

string1[j] = string2[i];

j++;

i++;

}

string1[j] = ‘\0’;

printf(“After Concatenation First String is %s.\n”, string1);

getch();

}

PROGRAM TO CONCATENATE TWO STRINGS USING STRCAT FUNCTION

#include<stdio.h>

#include<conio.h>

#include<string.h>

// Declaration for Maximum size of the String

#define MAX 100

void main()

{

char string1[MAX], string2[MAX];

clrscr();

printf(“Enter First String.\n”);

gets(string1);

printf(“First String is %s.\n”, string1);

printf(“Enter Second String.\n”);

gets(string2);

printf(“Second String is %s.\n”, string2);

// Code for concatenating two Strings

strcat(string1, string2);

printf(“After Concatenation First String is %s.\n”, string1);

getch();

}

C PROGRAM TO CONCATENATE TWO STRINGS USING FUNCTION:

#include<stdio.h>

#include<conio.h>

#include<string.h>

// Declaration for Maximum size of the String

#define MAX 100

// Declaration of Concatenating Function

void string_concatenate(char *, char *);

void main()

{

char string1[MAX], string2[MAX];

clrscr();

printf(“Enter First String.\n”);

gets(string1);

printf(“First String is %s.\n”, string1);

printf(“Enter Second String.\n”);

gets(string2);

printf(“Second String is %s.\n”, string2);

// Calling Concatenating Function

string_concatenate(string1, string2);

printf(“After Concatenation First String is %s.\n”, string1);

getch();

}

// Definition of Concatenating Function

void string_concatenate(char *str1, char *str2)

{

int i, j, length;

length = strlen(str1);

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

str1++;

while(*str2 != ‘\0’)

{

*str1 = *str2;

str1++;

str2++;

}

*str1 = ‘\0’;

}

OUTPUT OF THE PROGRAM:

Enter First String.
Hello
First String is Hello.
Enter Second String.
World
Second String is World.
After Concatenation First String is HelloWorld.
C program output of concatenating two strings

C PROGRAM TO COPY A STRING INTO ANOTHER STRING

#include<stdio.h>

#include<conio.h>

#define MAX 100

void main()

{

int i;

char string1[MAX], string2[MAX];

clrscr();

printf(“Enter the String.\n”);

gets(string1);

printf(“The First String is %s.\n”, string1);

i = 0;

while(string1[i] != 0)

{

string2[i] = string1[i];

i++;

}

printf(“The Second String is %s.\n”, string2);

getch();

}

C PROGRAM TO COPY A STRING USING STRCPY FUNCTION:

#include<stdio.h>

#include<conio.h>

#include<string.h>

#define MAX 100

void main()

{

char string1[MAX], string2[MAX];

clrscr();

printf(“Enter the String.\n”);

gets(string1);

printf(“The First String is %s.\n”, string1);

strcpy(string2, string1);

printf(“The Second String is %s.\n”, string2);

getch();

}

C PROGRAM TO COPY A STRING INTO ANOTHER STRING USING FUNCTION:

#include<stdio.h>

#include<conio.h>

void string_copy(char *str1, char *str2);

#define MAX 100

void main()

{

char string1[MAX], string2[MAX];

clrscr();

printf(“Enter the String.\n”);

gets(string1);

printf(“The First String is %s.\n”, string1);

string_copy(string1, string2);

printf(“The Second String is %s.\n”, string2);

getch();

}

void string_copy(char *str1, char *str2)

{

while(*str1 != ‘\0’)

{

*str2 = *str1;

str2++;

str1++;

}

*str2 = ‘\0’;

}

OUTPUT OF THE PROGRAM:

Enter the String.
Elephant
The First String is Elephant.
The Second String is Elephant.
C program output of copying a string to another string

Related Posts

  • C PROGRAM TO PRINT REVERSE OF STRINGC PROGRAM TO PRINT REVERSE OF STRING // C PROGRAM TO PRINT REVERSE OF STRING // DATE: 06-07-2015 #include<stdio.h> #include<conio.h> #include<string.h> void main() {             int length, i, […] Posted in C PROGRAM
  • C PROGRAM TO CHECK PALINDROMEC PROGRAM TO CHECK PALINDROME C PROGRAM TO CHECK A WORD PALINDROME A string is said to be palindrome if string can be read same from both side. For example string  aba is Palindrome while abc is not a Palindrome […] Posted in C PROGRAM
  • C PROGRAM TO DELETE ALL VOWELS FROM STRINGC PROGRAM TO DELETE ALL VOWELS FROM STRING // C PROGRAM TO DELETE ALL VOWELS FROM STRING // DATE: 09-07-2015 #include<stdio.h> #include<conio.h> #include<string.h> // Declaration of Maximum Length of the […] Posted in C PROGRAM
  • C PROGRAM TO SWAP TWO STRINGSC PROGRAM TO SWAP TWO STRINGS Swapping means interchanging contains of two variables. Now swapping of strings mean exchanging the word or letter stored in two variables. For example if a variable x contains a name […] Posted in C PROGRAM