Ive been researching all day on how to merge arrays, and make functions with variable parameters. Then it got me thinking, 'can't I combine the two?'. I came up with this function. According to my understanding it should work, but I'm getting errors. Can anyone tell me what I'm doing wrong?
#include <stdio.h>
#include <stdarg.h>
char* merge(int num, ...)
{
va_list list;
char arr[9] = {0};
char *temp;
int i;
int j;
int k=0;
va_start(list,num);
for(i=0;i<num;i++)
{
temp = va_arg(list,char[]);
j = 0;
while(temp[j] != 0x00)
{
arr[k] = temp[j];
j++;
}
k++;
}
va_end(list);
return arr;
}
int main()
{
char data_1[] = "my";
char merged_array[9] = "legs";
int n=0;
//merged_array = merge(1, data_1);
while(merged_array == 0x00)
{
printf("%s\n",merged_array[n]);
n++;
}
}
Perhaps this will help get you started:
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
char* merge(char *arr, int num, ...)
{
va_list list;
int i;
va_start(list,num);
for(i=0;i<num;i++)
strcat(arr, va_arg(list,char *));
va_end(list);
return arr;
}
int main()
{
char data_1[] = "my";
char merged_array[9] = "legs";
merge(merged_array, 1, data_1);
printf("%s\n", merged_array);
return(0);
}
Related
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct kezdo
{
int mennyi;
char betu;
}KEZDO;
int main(int argc, char* argv[])
{
int j;
int i;
int db=0;
int volt=0;
char sajt[22];
FILE* f=fopen(argv[1], "r");
if(f==NULL)
{
fprintf(stderr, "Hiba a fajl megnyitasaban!");
}
int k = 20;
KEZDO t[k];
KEZDO tmp;
for(i=0;i<k;i++)
{
t[i].mennyi = 0;
}
while(fgets(sajt,22,f)!=0)
{
if(sajt[strlen(sajt)-1] == '\n')
{
sajt[strlen(sajt)-1] = '\0';
}
for(i=0;i<k;i++)
{
if(t[i].betu == toupper(sajt[0]))
{
t[i].mennyi++;
volt=1;
}
}
if(volt==0)
{
t[db].betu = toupper(sajt[0]);
t[db].mennyi++;
db++;
}
else
{
volt = 0;
}
}
for(i=0;i<db;i++)
{
printf("%c: %d\n", t[i].betu, t[i].mennyi);
}
return 0;
}
I tried strcmp and stricmp but neither worked. I tried to fully change the struct by sorting the struct properties. When the struct properties are sorted it doesn't work, but it worked before in a non-sorted order. What is preventing output when the struct properties are sorted?
As i can see in your code, you want to sort on char betu. One way to sort structures is via qsort but that'd require comparator function stated below:
int compare(const void *void_a, const void *void_b)
{
const KEZDO *a = void_a;
const KEZDO *b = void_b;
return (a->betu) < (b->betu);
}
//Perform sort like this;
qsort((void *) &t, db, sizeof(KEZDO) , compare );
Moreover, qsort is in #include <stdlib.h>
I have the following code but the result is null for all components of the structure:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _TransactionType
{
char field1[20];
char field2[20];
}TransactionType;
int main(int argc, char *argv[]) {
int i;
int numreg = 0;
char temp[12];
TransactionType *dbTransaction;
dbTransaction = (TransactionType*) calloc(10,sizeof(TransactionType));
for(i=0; i<5;i++)
{
memset(temp,0,sizeof(temp));
sprintf(temp,"%d",i);
strcpy(dbTransaction->field1, temp);
dbTransaction->field1[strlen(dbTransaction->field1)] = '\0';
strcpy(dbTransaction->field2, temp);
dbTransaction->field2[strlen(dbTransaction->field2)] = '\0';
numreg++;
dbTransaction++;
}
printf("reg = %d\n", numreg);
for (i=0; i<numreg;i++)
{
printf("dbTransaction->field1 = %s\n",(dbTransaction + i)->field1);
printf("dbTransaction->field2 = %s\n",(dbTransaction + i)->field2);
}
return 0;
}
i need to recover the structure values.
Please any kind of help will be appreciate
Thanks in advance for your help
You should add error checking and casting of calloc values is discouraged, but the reason your code doesn't work is that you advance dbTransaction pointer in your loop, but never rewind it. The prints you're making are actually of elements 5-9 of the array while you fill elements 0-4.
See the corrected code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _TransactionType
{
char field1[20];
char field2[20];
}TransactionType;
int main(int argc, char *argv[]) {
int i;
int numreg = 0;
char temp[12];
TransactionType *dbTransaction;
TransactionType *dbTransactionRoot;
dbTransaction = (TransactionType*) calloc(10,sizeof(TransactionType));
dbTransactionRoot = dbTransaction;
for(i=0; i<5;i++)
{
memset(temp,0,sizeof(temp));
sprintf(temp,"%d",i);
strcpy(dbTransaction->field1, temp);
dbTransaction->field1[strlen(dbTransaction->field1)] = '\0';
strcpy(dbTransaction->field2, temp);
dbTransaction->field2[strlen(dbTransaction->field2)] = '\0';
numreg++;
dbTransaction++;
}
printf("reg = %d\n", numreg);
for (i=0; i<numreg;i++)
{
printf("dbTransaction->field1 = %s\n",(dbTransactionRoot + i)->field1);
printf("dbTransaction->field2 = %s\n",(dbTransactionRoot + i)->field2);
}
return 0;
}
I got this part of a C program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *mRNA = spleissen("AUAGUAAAAGCUCUGUUUAGGAGA", "GU", "AG");
printf("mRNA: %s\n", mRNA);
free(mRNA);
return 0;
}
I have to write the function spleissen which should work like this: it cuts out a string which goes from a GU to an AG and everything in between those two. So the program output is:
mRNA: AUACUCUGAGA
I don't really know how I can cut those parts out.
I am not allowed to use includes other than stdio, string and stdlib.
char *spleissen(const char *src, const char *start, const char *end){
size_t len = strlen(src);
char *s, *e, *ret, *work;
ret = work = malloc(len + 1);
strcpy(work, src);
len = strlen(end);
while(s = strstr(work, start)){
if((e = strstr(s, end))==NULL)
break;//delete upto last?
memmove(s, e + len, strlen(e+len)+1);
work = s;
}
return ret;
}
I think you can simply do this:
char *spleissen(char *array, char *G, char *A)
{
int l=strlen(array);
int i, j=0;
char returnstr[10010];
int b=0;
for(i=0; i<l; i++)
{
if(G[0]==array[i] && G[1]==array[i+1])
{
b=1, i++;
continue;
}
else if(A[0]==array[i] && A[1]==array[i+1] && b==1)
{
b=0, i++;
continue;
}
if(b==0)
{
returnstr[j]=array[i];
j++;
}
}
return returnstr;
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
char* strreverse(char*);
int main()
{
char *rev_string;
char *name="computer";
clrscr();
rev_string=strreverse(name);
printf("%s", rev_string);
getch();
return 0;
}
char* strreverse(char *name)
{
int length=strlen(name);
char *ptr;
char *rstr;
for(ptr=name+(length-1);ptr>=name;ptr--)
{
*rstr=*ptr;
printf("%c",rstr);
rstr++;
}
*(rstr)=NULL;
return rstr;
}
the above is my code. i tried to write a program for string reverse without using arrays. But i am not getting the output retupmoc. what is wrong in my code? how to insert null char in char*?
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
char* strreverse(const char*);
int main(){
char *rev_string;
char *name="computer";
clrscr();
rev_string=strreverse(name);
printf("%s\n", rev_string);
free(rev_string);
getch();
return 0;
}
char* strreverse(const char *name){
int length=strlen(name);
const char *ptr;
char *ret, *rstr = malloc(length + 1);
if(ret=rstr){
for(ptr=name+length;ptr != name;){
*rstr++ = *--ptr;
}
*rstr = '\0';
}
return ret;
}
You did not allocate memory to hold your reversed string. Try
char *rstr = calloc(1, length+1);
Also it should be
printf("%c", *rstr); // dereference
*(rstr)= '\0'; // instead of NULL
Here you find sweet and short solution for string reverse:
#include<stdio.h>
#include<string.h>
int strreverse(char* , char*);
int main()
{
char rev_string[10] = {0};
char name[10]="computer";
strreverse(name, rev_string);
printf("%s\n", rev_string);
return 0;
}
int strreverse(char *name, char *rStr)
{
int i = 0;
int length = strlen(name);
while(i < length)
{
rStr[i] = name[length-i-1];
i++;
}
return 0;
}
Try to run and have fun.
I wonder if I'm doing something wrong in my program.
I manage to create a HashTable but when I send it through parameter to my displayingList() function, it crashes.
source.c (contains my functions):
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#include "header.h"
#define MAX 255
int countLetters(char myStr[])
{
int myLen = strlen(myStr), i;
int wordLen = 0;
for (i = 0 ; i < myLen; ++i)
{
wordLen += (int)(myStr[i]);
}
return (wordLen%256);
}
void populateList(NodeT *T[255], char myStr[])
{
NodeT *p, *q;
p = (NodeT *)malloc(sizeof(NodeT));
strcpy (p->key, myStr);
int myPos = countLetters(myStr);
if(T[myPos] == NULL)
{
p->next = NULL;
T[myPos] = p;
}
else
{
q = T[myPos];
p->next = q;
T[myPos] = p;
}
}
void displayList(NodeT *T[255])
{
int i;
NodeT *p;
for(i = 0 ; i < 255; ++i)
{
if(T[i] != NULL)
{
printf("Index: %d - Data:", i);
p = T[i];
while(p != 0)
{
printf("%s, ", p->key); // HERE IT CRASHES.
p = p->next;
}
printf("\n");
}
}
}
main.c (contains the int main()):
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(void)
{
NodeT *T[255];
int n, i;
printf("Give no. of elements:");
scanf("%d", &n);
fflush(stdin);
for(i = 0 ; i < n ; ++i)
{
char name[100];
gets(name);
populateList(T, name);
}
displayList(T);
return 0;
}
header.h (and my header):
#ifndef HEADER_H
#define HEADER_H
typedef struct cell
{
char key[100];
struct cell *next;
}NodeT;
int countLetters(char myStr[]);
void populateList(NodeT *T[], char myStr[]);
void displayList(NodeT *T[]);
#endif // HEADER_H
I tried to see what exactly happens with debugger and it seems that when I send T[] list to displayList() function, actually it doesn't have the same structure as it has in main.c.
ISSUE: the insertion works fine, but when I try to display my list (on each index) it crashes.
Any ideas?
Thanks in advance.
The possible solution is to declare the NodeT *T[255] global. However it isn't the best practice at all.