Reputation: 65
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int x,y;
char s1[30],s2[30];
printf("Enter string 1:");
scanf("%s",s1);
printf("Enter string 2:");
scanf("%s",s2);
x=sizeof(s1),y=sizeof(s2);
char cs1[x+y+1];
char cs2[x+y+1];
for (int i = 0; i < x; i++)
{
cs1[i]=s1[i];
}
for (int i =x; i < (x+y); i++)
{
cs1[i]=s2[i];
}
for (int i = 0; i < (y); i++)
{
cs2[i]=s2[i];
}
for(int i=y;i<(x+y);i++)
{
cs2[i]=s1[i];
}
cs1[x+y]='\0';
cs2[x+y]='\0';
printf("string 1+string 2 is:%s",cs1);
printf("\nstring 2+ string 1 :%s",cs2);
return 0;
}
i tried to concatenate two strings without using the inbuilt functions to do this, can you please point the error out? this was giving the strings as they are i mean to say it is printing s1 string in place of cs1 and s2 in place of cs2
Upvotes: 0
Views: 76
Reputation: 310980
These assignments:
x=sizeof(s1),y=sizeof(s2);
do not make a sense because the assigned values do not represent lengths of entered strings.
You need to write:
#include <string.h>
//...
size_t x,y;
char s1[30],s2[30];
printf("Enter string 1:");
scanf("%s",s1);
printf("Enter string 2:");
scanf("%s",s2);
x = strlen(s1), y = strlen(s2);
//...
These for loops:
for (int i =x; i < (x+y); i++)
{
cs1[i]=s2[i];
}
//...
for(int i=y;i<(x+y);i++)
{
cs2[i]=s1[i];
}
are incorrect. You need to write:
for ( size_t i = 0; i < y; i++)
{
cs1[i + x] = s2[i];
}
//...
for( size_t i = 0; i < x; i++)
{
cs2[i + y ] = s1[i];
}
Upvotes: 2
Reputation: 551
Oh well, an answer got accepted just before I could post my version, but just for reference I'll paste it anyway.
As others pointed out, there were a number of errors in your code:
sizeof(s)
would always yield 30, you have to use a function defined by yourself if you want to easily count the length of a stringfor
, you were using the wrong index when copying the stringscanf
to fgets
so that you could accept strings with spaces too. And I added a #define MAX_LENGTH 30
at the top, so that you could easily re-purpose the program for shorter/longer inputsAnyway, here's the code
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 30 /* use defines instead of repeating the same number by hand */
int myStrLen(char *s, int max_len) {
/* this little function counts the lenght of the passed string, up to max_len */
int len = 0;
for (int i = 0; i < max_len; i++) {
if (s[i] != '\0' && s[i] != '\n') {
len++;
} else {
break;
}
}
return len;
}
int main() {
int x, y;
char s1[MAX_LENGTH], s2[MAX_LENGTH];
printf("Enter string 1:");
fgets(s1, MAX_LENGTH, stdin); /* using fgets instead of scanf allows reading strings containing spaces */
//scanf("%s",s1);
printf("Enter string 2:");
fgets(s2, MAX_LENGTH, stdin);
//scanf("%s",s2);
x = myStrLen(s1, sizeof(s1));
y = myStrLen(s2, sizeof(s2));
printf("string1 length: %d\nstring2 length: %d\n", x, y);
char cs1[x + y + 1];
char cs2[x + y + 1];
for (int i = 0; i < x; i++) {
cs1[i] = s1[i];
}
for (int i = 0; i < y; i++) {
cs1[i+x] = s2[i]; // nb: (i+x) instead of i
}
for (int i = 0; i < y; i++) {
cs2[i] = s2[i];
}
for (int i = 0; i < x; i++) {
cs2[i+y] = s1[i]; // // nb: (i+y) instead of i
}
cs1[x + y] = '\0';
cs2[x + y] = '\0';
printf("string1+string2 is:%s\n", cs1);
printf("string2+string1 is:%s\n", cs2);
return 0;
}
Upvotes: 0
Reputation: 67546
x=sizeof(s1),y=sizeof(s2);
sizeof does not give you the length of the string
Use function strlen
instead.
Your for
loops are also wrong
for (size_t i = 0; i < x; i++)
{
cs1[i]=s1[i];
}
for (int i = x; i < (x+y); i++)
{
cs1[i]=s2[i - x];
}
for (int i = 0; i < (y); i++)
{
cs2[i]=s2[i];
}
for(int i=y;i<(x+y);i++)
{
cs2[i]=s1[i - y];
}
Upvotes: 1
Reputation: 36
Try this, that work for me and I created a function "my_strlen" to get the size of your string
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int my_strlen(char *str)
{
int i;
for (i = 0; str[i] != '\0'; i++);
return (i);
}
int main()
{
int x,y;
char s1[30],s2[30];
printf("Enter string 1:");
scanf("%s",s1);
printf("Enter string 2:");
scanf("%s",s2);
x=my_strlen(s1),y=my_strlen(s2);
char cs1[x+y+1];
char cs2[x+y+1];
for (int i = 0; s1[i] != '\0'; i++)
{
cs1[i]=s1[i];
}
for (int i = 0; s2[i] != '\0'; i++)
{
cs1[i + x] = s2[i];
}
for (int i = 0; s2[i] != '\0'; i++)
{
cs2[i] = s2[i];
}
for(int i = 0; s1[i] != '\0'; i++)
{
cs2[i + y] = s1[i];
}
cs1[x+y]='\0';
cs2[x+y]='\0';
printf("string 1+string 2 is:%s",cs1);
printf("\nstring 2+ string 1 :%s",cs2);
return (0);
}
Upvotes: 2