I have some basic question about the pointers, I have a char array and I am extracting some words my char array with using function and I want to return value and print it main I have Code But it's not working, thanks for your help
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char cumle[30];
char kelimecikart(char *cumle,char *sozcuk);
int main(){
int i;
char sozcuk[30];
printf("sentence:\n");
gets(cumle);
puts(cumle);
printf("What is the word you want to extract of:\n");
gets(sozcuk);
puts(sozcuk);
printf("\n");
cumle[0]=kelimecikart(cumle,sozcuk);
for(i=0;i<17;i++){
printf("%c",cumle[i]);
}
}
char kelimecikart(char *dizi,char *cikansoz)
{
int a = strlen(dizi);
int b = strlen(cikansoz);
int i,j,tmp=0;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
if(*(dizi+i+j)==*(cikansoz+j)){
tmp++;
}
else{
break;
}
}
if(tmp==b){
i+=tmp-1;
}
else{
*(cumle+i)=*(dizi+i);
}
tmp=0;
}
return *cumle;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char cumle[30];
void kelimecikart(char *cumle,char *sozcuk);
int main(){
int i;
char sozcuk[30];
printf("sentence:\n");
gets(cumle);
puts(cumle);
printf("What is the word you want to extract of:\n");
gets(sozcuk);
puts(sozcuk);
printf("\n");
kelimecikart(cumle,sozcuk);
for(i=0;i<17;i++){
printf("%c",cumle[i]);
}
}
void kelimecikart(char *dizi,char *cikansoz)
{
int a = strlen(dizi);
int b = strlen(cikansoz);
int i,j=1;
for(i=0;i<a;){
if(j==0){
i++;
}
else if(j==b){
break;
}
for(j=0;j<b;j++){
if(dizi[i]==cikansoz[j]){
i++;
}
else{
break;
}
}
}
for(;i<a;i++){
dizi[i-b]=dizi[i];
}
return ;
}
//
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>
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.
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);
}
This function stops awsering, and I can't spot the prob, can someone try to find it please?
It is suposed to give me the designed name and number from an array of strings.
#include <stdio.h>
#include <string.h>
int sameName();
char **getNumber();
char **getNumber (char *n[], char e[],int N){
int a;
for(a=0;a<N;a++){
if (sameName(n[a],e))
{
return n[a];
}
}
return "Not found!";
}
int sameName(char n[], char e[]){
int a;
for(a=0;e[a]!='\0';a++){
if (n[a]!=e[a])
{
return 0;
}
}
return 1;
}
int main (){
char numbers [5] [100] ={{"Ash 031"},{"Bomberman 021"},{"Rango 120"},{"Gigo Senhas 017"},{"Marcoreano 135"}};
char name [100];
char a [100];
scanf("%s",&a);
strcpy(name,getNumber (numbers,a,5));
printf("%s\n",name);
return 0;
}
You need to spicify the size of your two-dimension array when you pass it to your getNumber function. See http://c-faq.com/aryptr/pass2dary.html for details.
And for scanf you only need to pass a since it is a string.
Taking these plus fixing your prototypes as mentioned in the comment above, that gives :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sameName(char n[], char e[]);
char *getNumber (char n[][100], char e[],int N);
char *getNumber (char n[][100], char e[],int N){
int a;
for(a=0;a<N;a++){
if (sameName(n[a],e))
{
return n[a];
}
}
return "Not found!";
}
int sameName(char n[], char e[]){
int a;
for(a=0;e[a]!='\0';a++){
if (n[a]!=e[a])
{
return 0;
}
}
return 1;
}
int main (){
char numbers [5] [100] ={{"Ash 031"},{"Bomberman 021"},{"Rango 120"},{"Gigo Senhas 017"},{"Marcoreano 135"}};
char name [100];
char a [100];
scanf("%s",a);
strcpy(name,getNumber (numbers,a,5));
printf("%s\n",name);
return 0;
}
#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.