Q.Write a function to concatenate to input strings into one large string and display the result.
I know how to do this using strcat but not without it. Also can't use pointers. Please help!!
Q.Write a function to concatenate to input strings into one large string and display the result.
I know how to do this using strcat but not without it. Also can't use pointers. Please help!!
hi it is very easy find the length of the first character array, and find the length of the second character array, using a loop concodinate both strings.
i dont know it is correct or not
char str1[],str2[];
int l1,l2;
l1=length(str1);
l2=length(str2);
for(i=0 ;i<str2;i++)
str1[l1+i]=str2;
>i dont know it is correct or not
It's not. Why don't you test out thing on your compiler first before posting them?
To concatenate a string, remember that strings are basically arrays of characters. This means that you can loop through the arrays (or strings) and copy letters into another one. First of all, create an array that's the combined length of both strings (plus 1 for the terminating null character). Then simply start by copying the first string into the array, and then proceed by doing the same to the other.
/* myconcat.c */
#include <stdio.h>
int main(void)
{
char first_s[] = "Hello";
char second_s[] = "World";
int i = 0;
int x = 0;
char long_s[sizeof(first_s) + sizeof(second_s)];
while(first_s[i])
{
long_s[i] = first_s[i];
++i;
}
long_s[i] = ' ';
++i;
while(second_s[x])
{
long_s[i] = second_s[x];
++i;
++x;
}
long_s[i] = '\0'; /* don't forget the null */
printf(long_s);
putchar('\n');
getchar();
return 0;
}
If you want to learn something more advanced, do read this little snippet by Dave Sinkula.
Hey I got it..i did it differently though..thnx neways :)
sorry i am now using vb very much but i have used c only 2 years befor. i was in a cafe so i was unable to compile the program...
sorry i am now using vb very much but i have used c only 2 years befor. i was in a cafe so i was unable to compile the program...
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
int l1,l2,i;
clrscr();
gets(str1);
gets(str2);
l1=strlen(str1);
l2=strlen(str2);
for(i=0;i<=l2;i++)
{
str1[l1+i]=str2[i];
}
printf("%s",str1);
}
Or u could use a Linked List to put the first string in a List and put the second string in another list .
Then u can link the two lists together to get A concatenated string :-O .
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
int l1,l2,i;
clrscr();
gets(str1);
gets(str2);
l1=strlen(str1);
l2=strlen(str2);for(i=0;i<=l2;i++)
{
str1[l1+i]=str2;
}
printf("%s",str1);
}
The main function always returns an integer when hosted by an operating system. So void main() is not acceptable.
gets() is not acceptable neither, for different reason. It can not be stop of reading more that buffer can support, without overflow. #include<conio.h>
and clrscr();
are none portable, therefore voids the "warranty". Furthermore, clearing the screen is a doubtful practice, for the sake of "prettiness".
Next time use the expected code tags to post source code. Here's an example.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
int l1,l2,i;
clrscr();
gets(str1);
gets(str2);
l1=strlen(str1);
l2=strlen(str2);for(i=0;i<=l2;i++)
{
str1[l1+i]=str2;
}
printf("%s",str1);
}
how if i want to add the string to the front??
Just swap every variable below the strlen() functions.
l1 <=> l2
str1 <=> str2
Also:
You should declare main as returning an int, not void.
And you should NOT use gets(), use fgets() instead.
Do some reading on why, it's quite interesting and important to know.
Here's the simple version of the program:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
char arr1[] = "Ashutosh";
char arr2[] = " Thakur";
cout<<"SizeOf 1st: "<<sizeof arr1<<endl; //Get the size of first string
cout<<"SizeOf 2nd: "<<sizeof arr2<<endl; //Get the size of second string
int size = (sizeof arr1 + sizeof arr2);
cout<<"Size of 3rd: "<<size<<endl;
char arr3[sizeof arr1 + sizeof arr2]; //size of third string
for(int i=0;i<sizeof arr1;i++) //Run loop from 0 to size of 1st arr
{
arr3[i] = arr1[i];
}
for(int j=(sizeof arr1-1),k=0;j<17,k<sizeof arr2; j++,k++) //Run loop from size end loop to size of 3nd array
{ //Run loop from 2nd array to last of its size
arr3[j] = arr2[k];
}
for(int k=0;k<sizeof arr3; k++)
{
cout<<arr3[k];
}
system("pause");
}
I guess you are asking a code not using "string.h" header file.
I think this will help. Code is below:
#include<stdio.h>
int xstrlen(char *);
void xstrcat(char *,char *,int);
void main()
{
char source[]="Sarker";
char target[30]="Maruf";
int j=xstrlen(target);
xstrcat(target,source,j);
printf("Source String: %s\nTarget String: %s",source,target);
}
int xstrlen(char *s)
{
int len=0;
while(*s!='\0')
{
len++;
s++;
}
return len;
}
void xstrcat(char *t,char *s,int j)
{
while(*t!='\0')
{
*t=*t;
t++;
}
while(*s!='\0')
{
*t=*s;
s++;
t++;
}
}
t=t;
This accomplishes a whole lot of nothing. All you're doing is simulating the effect of strlen() without the overhead of a function call, so no assignments are necessary. Just bump the value of t
until it's pointing to a null character:
while (*t)
++t;
You also don't use j, but presumably the idea was to provide a limit if the source would overflow the destination. In that case you'd be duplicating the functionality of strncat():
char *xstrcat(char *dst, const char *src, size_t n)
{
char *p = dst;
while (*p)
++p;
while (n-- && *src)
*p++ = *src++;
*p = '\0';
return dst;
}
Though it's also possible you intended to use it as a starting position instead of calculating it internally:
char *xstrcat(char *dst, const char *src, size_t pos)
{
char *p = dst + pos;
while (*p++ = *src++)
;
return dst;
}
I'll let someone else browbeat you for using void main, but I'll mention that your variable names are too concise to make for readable code. t
, s
, and j
are all pretty much meaningless; single character variables should be reserved for the most common of uses. Included in that list might be:
But unless the variables are used in a context where longer names would distract readers, you should prefer names that are short but meaningful as to what the variable does.
hi iMARUF & deceptikon
iMARUF code can used after i change it
and make strcat for two strings
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int xstrlen(char *);
char* xstrcat(char *,char *);
void main()
{
char source[]="Sarker";
char target[]="Maruf";
char*TotalName;
clrscr();
TotalName=xstrcat(target,source);
printf("Source String: %s\nTarget String: %s\n",source,target);
printf("Total String: %s",TotalName);
getch();
}
int xstrlen(char *s)
{
int len=0;
while(*s!='\0')
{
len++;
s++;
}
return len;
}
char* xstrcat(char *t,char *s)
{
int x,y,z;
int i,j=0,k;
char*name;
x=xstrlen(t);
y=xstrlen(s);
z=x+y+1;
name=(char*)malloc(z*sizeof(char));
for(i=0;i<x;i++)
{
*(name+i)=*(t+i);
}
*(name+x)=' ';
for(k=x+1;k<z;k++)
{
*(name+k)=*(s+j);
j++;
}
return name;
}
hi IwalkAlone
to make concatenate for two strings
you must make your own function which make string concatenate like strcat function
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.