Tuesday, 17 January 2017

Concatenate two strings in C programming(c programming examples)(c program examples)

Concatenate two strings in C programming

The program is an example to introduce the function 'strcat()' which is a predefined function in the header file 'string.h'. The meaning of concatenating is to connect the two string end by end.

If you want to see this code with a perfect indentation, copy the code into "sublime text editor" and make sure that the type of code is set to 'c' at the bottom right of the window. After pasting the code press the command "ctrl+shift+P", you get a search box. Type "indentation" you get an option like this below the search box "Indentation: Reindent lines". Clink on that to get the code with indentation. 

There are two methods to concatenate two strings:

     1. Without using string functions.
     2. With using string functions.

The program without using string functions:

#include <stdio.h>
main()
{
char text1[1000],text2[1000], output[1000];
int i,j;
printf("enter the first string\n");
gets(text1);
printf("enter the second string\n");
gets(text2);
for(i=0;text1[i]!='\0';i++)
output[i]=text1[i];
for(j=0;text2[j]!='\0';j++)
{
output[i]=text2[j];
i++;
}
printf("after concatention of two strings the output is as follows\n");
for(i=0;output[i]!='\0';i++)
printf("%c",output[i] );
return 0;
}


The program with using string functions:

#include <stdio.h>
#include <string.h>
main()
{
char text1[1000],text2[1000],output[1000];
printf("enter the first string\n");
gets(text1);
printf("enter the second string\n");
gets(text2);
strcpy(output,text1);
strcat(output,text2);
printf("after concatenation the output string is '%s'\n",output );
return 0;
}


The output is as follows:






#include <stdio.h>


The '#include' is a preprocessor directive which includes the source files inside the code. The 'stdio.h' is a standard input and output header file with several functions written in it. The functions like 'printf()''gets()' and 'scanf()' are work when we include 'stdio.h' header file. 

char text1[1000],text2[1000], output[1000];

We declare three arrays for the program. The array 'text1[]' is to hold the first string. The array 'text2[]' is to hold the second string. The array 'output[]' is to hold the output string after concatenation of two strings. And the data type 'char' is used to hold the character data in all the arrays which are declared.

int i,j;

The variable 'i', 'j' are used in loops to browse the elements in the arrays. And the data type 'int' is used to carry the integer values in variables.

printf("enter the first string\n");
gets(text1);
printf("enter the second string\n");
gets(text2);

The functions 'printf()' and 'gets()' are predefined function in the header file 'stdio.h'. The function 'printf()' is used to print the message "enter the first string" and "enter the second string" on the screen. The function 'gets()' is used to assign the string value into the arrays which are declared in the first line of the body of the program.

for(i=0;text1[i]!='\0';i++)
output[i]=text1[i];

This loop assigns each character in the array to the 'output[]' array. After completion of elements in the array 'text1[]', the loop terminates and the execution process gets into the next line.

for(j=0;text2[j]!='\0';j++)
{
output[i]=text2[j];
i++;
}

This loop assigns the elements in the array 'text2[]' into the array 'output[]' from the end where the process of assign is stopped in the previous loop.

printf("after concatenation the output string is '%s'\n",output );

After the process is completed the string is printed on the output screen with the help of the function 'printf()'.

In the second program in the place of two loops, we use the following functions.

STRCPY(): to copy the string into the another.
STRCAT(): to concatenate the second string into the first.

These two functions are predefined functions which are written in the header file 'string.h'. When we use this function we should include the header file 'string.h' to help the compiler to include functions in the file 'string.h'. Hence the problem is solved and another program gets into the bucket.



C is a general-purpose programming language. It has been closely associated with the UNIX system where is was developed, since both the system and most of the programs that run on it are written in C. The language, however, is not tied to any one operating system or machine; and although it has been called a “system programming language” because it is useful for writing compilers and operating systems, it has been used equally well to write major programs in many different domains.(c programming examples)(c program examples)
Many of the important ideas of C stem from the language BCPL, developed by Martin Richards. The influence of BCPL on C proceeded indirectly through the language B, which was written by Ken Thompson in 1970 for the first UNIX system on the DEC PDP-7.(c programming examples)
BCPL and B are “typeless” languages. By contrast, C provides a variety of data types. The fundamental types are characters, and integers and floating point numbers of several sizes. In addition, there is a hierarchy of derived data types created with pointers, arrays, structures, and unions. Expressions are formed from operators and operands; any expression, including an assignment or a function call, can be a statement. Pointers provide for machine-independent address arithmetic.(c programming examples)(c program examples)
C provides the fundamental control-flow constructions required for well-structured programs: statement grouping, decision making (if-else), selecting one of a set of possible cases (switch), looping with the termination test at the top (while, for) or at the bottom (do), and early loop exit (break).(c programming examples)(c program examples)
Functions may return values of basic types, structures, unions, or pointers. Any function may be called recursively. Local variables are typically “automatic,” or created anew with each invocation. Function definitions may not be nested but variables may be declared in a block-structured fashion. The functions of a C program may exist I separate source files that are compiled separately. Variables may be internal to a function, external but know only within a single source file, or visible to the entire program.(c programming examples)(c program examples)
A preprocessing step performs macro substitution on program text, inclusion of other source files, conditional compilation.(c programming examples)(c program examples)
C  is a relatively “low level” language. This characterization is not pejorative; it simply means that C deals with the same sort of object that most computers do, namely characters, numbers, and addresses. These may be combined and moved about with the arithmetic and logical operators implemented by real machines.(c programming examples)(c program examples)
C provides no operations to deal directly with composite objects such as character strings, sets, lists, or arrays. There are no operations that manipulate an entire array or string, although structures may be copied as a unit. The language does not define any storage allocation facility other than static definition and the stack discipline provided by the local variables of functions; there is n heap or garbage collection. Finally, C itself provides no input/output facilities; there are no READ or WRITE statements, and no built-in file access methods. All of these higher-level mechanisms must be provided by explicitly called functions. Most C implementations have included a reasonably standard collection of such functions.(c programming examples)(c program examples)




1 comment :

  1. It's very useful blog post with informative and insightful content and i had good experience with this information.I have gone through CRS Info Solutions Home which really nice.Find this real time DevOps Training and great teaching. Join now on Selenium Training online course. Upskill career with Tableau training by crs info solutions. Latest trending course is Salesforce Lightning training with excellent jobs.

    ReplyDelete