| | |
How to concatenate two strings without using strcat?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
>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.
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.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
C Syntax (Toggle Plain Text)
/* 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.
http://www.daniweb.com/code/snippet406.html
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
•
•
Join Date: Feb 2009
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
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);
}
•
•
•
•
#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);
}
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.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
![]() |
Similar Threads
- Need Help With Some Programs (C)
- problem in concatenation of 2 strings (C)
- adding to a string (C++)
- File parsing in 'C' (C)
- Fun with Forms (PHP)
- Program runs in an infinite loop (C++)
Other Threads in the C Forum
- Previous Thread: conversion of decimal to binary from 1 to 1024
- Next Thread: Need help with simple C program
Views: 15123 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C
#include * array arrays asterisks binarysearch calculate changingto char cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkwinlinux hacking hardware histogram homework inches include incrementoperators input iso kernel keyboard km lazy linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue mysql number opendocumentformat opensource overwrite owf pattern pdf performance pointer pointers posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string structures student systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






. 