Error While Using String LIbrary Function - c

This is the error that I am facing.
ERROR: warning: passing argument 1 of ‘__builtin_strlen’ makes pointer from integer without a cast [-Wint-conversion]
Question Link:Hackerrank Balanced Brackets
My Code:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
char rev(char c){
char ch;
if(c=='[')
ch=']';
else if(c=='{')
ch='}';
else if(c=='(')
ch=')';
return ch;
}
int main(){
int t; //test cases
scanf("%d",&t);
for(int a0 = 0; a0 < t; a0++){
char* s = (char *)malloc(10240 * sizeof(char));
int l,ret;
scanf("%s",s);
l=strlen(s);
char *st;
st=(char *)malloc(l * sizeof(char));
int top=-1;
for(int i=0;i<l;i++){
char ch=s[i];
char chr=rev(ch);
ret=strcmp(st[top],chr);
if(ret!=0){
top++;
st[top]=ch;
}
else{
top=top-1;
}
}
if(top==-1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
I shall be really thankful if anybody can help me out.
Thanks in advance.

Related

Segmentation Fault naming constant with a function C

So I have a string like "11111 & 11111" and i need to write this char in the middle in (char operation), but i have Segmentation Fault.
main.c
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char getChar(char *a);
int main(){
char *str = NULL;
size_t n = 0;
int a = getline(&str,&n,stdin);
printf("%c",getChar(str));
char operation = getChar(str);
free(str);
return 0;
}
bin.c(compiling with main.c)
#include <stdio.h>
#include <string.h>
char getChar(char *a){
char *p = strtok(a," ");
while (p != NULL){
p = strtok(NULL," ");
break;
}
return p[0];
}
in main.c this one
printf("%c",getChar(str));
is works well
but when i write this one
char operation = getChar(str);
I get Segmentation Fault

Randomize letters in string in C

I have for example "asd" and I want it to be randomized to DAS, DSA, SAD, you know. How can I code this? Tried a few solutions but It didnt really work.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
int main()
{
printf("type in the word\n");
char haslo[128];
scanf("%s", haslo);
char set[128];
char hasloa[128];
strcpy(set, haslo);
unsigned int Ind = 0;
srand(time(NULL) + rand());
int len = strlen(set);
while(Ind < len)
{
hasloa[Ind++] = set[rand()%62];
}
hasloa[len] = '\0';
printf("%s", hasloa);
return 0;
}
Change 62 inside the while loop to "len"

What is wrong with my program reversing strings in C?

I want to create a function to reverse a string in C. I found a couple pre-made on the internet but I wish to create mine. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* inverseCarac(char* chaine){
if(chaine==0||*chaine==0) return NULL;
int j, i;
int taille=strlen(chaine);
char* inverse=malloc(taille*sizeof(char));
for(j=taille-1, i=0; j>0; j--, i++){
*(inverse+i)=*(chaine-j);
}
return inverse;
}
int main(void){
char* test="bonjour";
char* inv=inverseCarac(test);
printf("%s", inv);
return 0;
}
I can't figure out why I get a segmentation fault.
There were several errors in your code, the most significant being the offset from chaine in the wrong direction. Also, lack of space for a string terminator, and j ending prematurely.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* inverseCarac(char* chaine){
if(chaine==0||*chaine==0) return NULL;
int j, i;
int taille=strlen(chaine);
char* inverse=malloc(taille+1); // add 1 for terminator
for(j=taille-1, i=0; j>=0; j--, i++){ // change j>0 to j >= 0
*(inverse+i)=*(chaine+j); // change -j to +j
}
inverse[taille] = '\0'; // write terminator
return inverse;
}
int main(void){
char* test="bonjour";
char* inv=inverseCarac(test);
printf("%s\n", inv);
return 0;
}
Program output
ruojnob

How to merge arrays passed to a function with variable parameters

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);
}

Error while executing: Segmentation fault

I'm a beginner in C language. After reading the initial chapters of Ritchie's book, I wrote a program to generate random numbers and alphabets.
The program compiles fine with gcc. However on running it, it gives an error "Segmentation fault", which is incomprehensible to my limited knowledge. I'd be glad to understand what I've written wrong.
#include <stdio.h>
#include <stdlib.h>
#include "conio.h"
#include <time.h>
long int genrandom(int,int);
void randAlph(void);
char letterize(int);
int main (void) {
// char full[9];
// char part_non[4];
srand(time(0));
int i;
for (i=0;i<50;++i) {
randAlph();
};
}
long int genrandom(int mino,int maxo) {
int val=mino+rand()/(RAND_MAX/(maxo-mino)+1);
return val;
}
void randAlph (){
int val;
char text;
val=genrandom(0,26);
// return val;
text=letterize(val);
printf("%s ,",text);
}
char letterize(int num) {
char letter='A'+num;
return letter;
}
printf("%s ,",text); is wrong - it says that text is a nul-terminated array of chars. Use
printf("%c ,", text);
instead to print your single char.
#include <stdio.h>
#include <stdlib.h>
#include "conio.h"
#include <time.h>
int genrandom(int,int);
void randAlph(void);
char letterize(int);
int main (void) {
// char full[9];
// char part_non[4];
srand(time(0));
int i;
for (i=0;i<50;++i) {
randAlph();
};
}
int genrandom(int mino,int maxo) {//changed function return type to int
int val=mino+rand()/(RAND_MAX/(maxo-mino)+1); //Be careful when you are using '/' operator with integers
return val; //returning int here why set return type to long int?
}
void randAlph (){
int val;
char text;
val=genrandom(0,26);
// return val;
text=letterize(val);
printf("%c ,",text);//Replace %s with %c
}
char letterize(int num) { //No bound checking on num eh?
char letter='A'+num;
return letter;
}
That's all I had to say. :)
Why use %s when text is char. You dont need a string type in the function. Just a char would do. Change in the function : void randAlph ()
printf("%s ,",text);
to
printf("%c ,", text);

Resources