August 2013 - Coding Bot

Like Us On Facebook

Follow Us On Twitter

Drop Down Menu

Tuesday, 6 August 2013

C Code To Concatenate Two Strings

Hello guys,
In this post, I'll explain the C Code To Concatenate Two Strings. 

As we all know that, the process of appending one string to the end of another string is called concatenation. If s1 and s2 are two strings and when s2 is appended to the end of s1, s1 and s2 are said to be concatenated. The string s1 then contains the original string plus the string contained in s2. Similar to copying and comparing, appending s2 to s1 should be done on a character by character basis. C provides strcat() built-in function to concatenate two strings.


The prototype of strcat() is as follows:

          strcat(s1,s2);

C Program To Concatenate Two Strings:

/* Double-Click To Select Code */

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

void main()
{ 
 char str1[20]="abc", str2[20]="def", str3[20]="ghi";
 int i,j;
 clrscr();

 printf("str1 = %s | str2 = %s \n",str1,str2);

/* Concatenation of str1 and str2 without using strcat() begins */
 
 for(i=0 ; str1[i]!='\0' ; i++);
 for(j=0 ; str2[j]!='\0' ; j++)
 {
  str1[i+j] = str2[j];
 }
 
 str1[i+j]='\0';
 printf("\nAfter concatenation str1 = %s \n",str1);

/* Concatenation of str1 and str2 without using strcat() begins */
 strcat(str1,str3);
 printf("\nstr1 = '%s' \n",str1);
 strcat(str1,"jkl");
 printf("\nstr1 = '%s' \n",str1);

 printf("\n\n *WWW.CODINGBOT.NET*");
 getch();
}

Program Explanation:

Str1, str2 and str3 are declared to be arrays of char type and all are initialized strings. The integer variables i and j are to traverse the strings. String in str2 is appended to the end of the string  in str1 by following segment of the program:

for(i=0 ; str1[i]!='\0' ; i++);
for(j=0 ; str2[j]!='\0' ; j++)
{
str1[i+j] = str2[j];
}
str1[i+j] = '\0';
The first for loop is to simply scan through the string str1. When the loop completes, the variable i points to the position of null character '\0' in str1. The second loop is to scan through the second string str2 till end of it is reached. When j takes 0, the first character in str2 is assigned to the position of null character in str1. So, the null character is str1 is overwritten by the first character in str2. Subsequently, the remaining characters in str2 are appended to str1 and lastly, the null character '\0' is assigned to the last position of str1. The string in str1, "abcdef" is then displayed.

Then two calls are mode to strcat(). In the final call, strcat(str1,str3), str3 is appended to str1. Since str3 had "ghi", sstr1 now becomes "abcdefghi" and is displayed. On the second call, strcat(str,"jkl"), the string constant "jkl" is appended to str1. The new string "abcdefghijkl" in str1 is again displayed.

Program Output:

C Code To Concatenate Two Strings


Please Comment If You Liked The Post.


Sunday, 4 August 2013

C Code To Find The Length Of a String

Hello guys,
In this post, I'll explain the C Code how to find the length of a string, by 2 methods, first by using strlen() function and second by normal programming.

Length of a string is defined to be the number of characters in it excluding the null character '\0'. To find the length of a string, we need to scan through the string, count the number of characters till the null character is reached. C provides a built-in function namely, strlen() to find the length of a string. 

C Program To Find The Length Of a String:


/* Double-Click To Select Code */

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

void main()
{ 
 char str[100];
 int i,length;
 clrscr();

 printf("Enter a String: ");
 gets(str);

/* Finding the length of str without using strlen() begins */

 length=0;
 for(i=0 ; str[i]!='\0' ; i++)
 length++;
 printf("\nLength of '%s' = %d\n",str,length);

/* Finding the length of str using strlen() begins */

 length = strlen(str);
 printf("Length of '%s' using strlen = %d \n",str,length);

 printf("\n\n *WWW.CODINGBOT.NET*");
 getch();
}

Program Explanation:

str is declared to be an array of char type and of size 100. Length and i are declared to be variables of int type. str is to collect the input string. Variable length is to collect the length of the string in str. The variable i is to scan through the string accessing each character.

A string is read into str through the keyboard  The following segment finds the length of str without strlen():

                          length = 0;
                          for(i=0 ; str[i] != '\0' ; i++)
                                  length++;

Length is initialized to 0 before scanning through the string begins. When the loop is entered, scanning through the string begins. During the course of scanning, if the character is not fund to be null character '\0', length is incremented by one. The loop is exited when the null character is reached. So, when the loop completes, length collects the length of str.
The length of str is found out using strlen() built-in function also.

Program Output:


C Code To Find The Length Of a String

Please Comment If You Liked The Post.