+ 1

can any one help me in my code

#include <stdio.h> #include <stdlib.h> #include <string.h> int longage2(char phe[ ]){ if (strlen(phe)==1||strlen(phe)%2!=0||phe[strlen(phe)]=='a'){ return 0; } for(int i=0;i<strlen(phe);i++){ if(i%2==0){ if(phe[i]!='a'){ return 0; }}else{ if(phe[i]!='b'){ return 0;}} }return 1;} void longage4(char phe[]){ int a;char ph1[10]; for( int i=0;i<=strlen(phe)-1;i++){ ph1[i]=phe[i]; printf("%d\n",longage2(ph1)); if(longage2(ph1)==1){ a=i; }} printf("a=%d\n",a); char ph2[a]; for(j=0;j<=a;j++){ ph2[j]=ph1[j]; } printf("%s\n",ph2); printf("%d",longage2(ph2)); } int main() {char phe[20]; printf("donnez un mot\n"); scanf("%d",phe); longage4(phe); return 0; }

21st Oct 2020, 9:15 PM
mar ia
mar ia - avatar
11 ответов
+ 5
mar ia the problem is that ph2 has no terminating character '\0' Also if you want ph2 to contain ph2[3] you should declare char ph2[4]; so char ph2[a+1]; adding another space for \0 it become: char ph2[ a + 2 ] for(j=0; j <= a; j++) { ph2[i] = ph1[i]; } ph2[a+1] = '\0';
21st Oct 2020, 10:37 PM
Davide
Davide - avatar
+ 4
The scanf in the main() should have %s And you need to declare int j; into longage4()
21st Oct 2020, 9:33 PM
Davide
Davide - avatar
+ 4
Can you tell me what to enter as input?
21st Oct 2020, 9:43 PM
Davide
Davide - avatar
+ 3
Thank you very much for helping me. I don't know😦 how to express my thanks to you😀😊
21st Oct 2020, 10:54 PM
mar ia
mar ia - avatar
+ 1
I fex it but wan yo but (donne un mot and you raet abab l hav proplem in resolt of line 32
21st Oct 2020, 9:37 PM
mar ia
mar ia - avatar
+ 1
He raet to me "abab?" I didnt no whay but (?)qustshen marc
21st Oct 2020, 9:39 PM
mar ia
mar ia - avatar
+ 1
#include <stdio.h> #include <stdlib.h> #include <string.h> int longage2(char phe[ ]){ if (strlen(phe)==1||strlen(phe)%2!=0||phe[strlen(phe)]=='a'){ return 0; } for(int i=0;i<strlen(phe);i++){ if(i%2==0){ if(phe[i]!='a'){ return 0; }}else{ if(phe[i]!='b'){ return 0;}} }return 1;} void longage4(char phe[]){ int a;char ph1[10];int j; for( int i=0;i<=strlen(phe)-1;i++){ ph1[i]=phe[i]; if(longage2(ph1)==1){ a=i; }} char ph2[a]; for(j=0;j<=a;j++){ ph2[j]=ph1[j]; } printf("%s\n",ph2); } int main() {char phe[20]; printf("donnez un mot\n"); scanf("%s",phe); longage4(phe); return 0; }
21st Oct 2020, 9:49 PM
mar ia
mar ia - avatar
+ 1
thes is beter an clen and the input is abab and mast be the reasolte is abab
21st Oct 2020, 9:50 PM
mar ia
mar ia - avatar
+ 1
The program works like this When we enter (ab)^n n>0 the reasolt is (ab)^n wen we but like exp : aba the resolt shod be :ab He is looking for (ab)^n Consecutive and he Display it
21st Oct 2020, 9:58 PM
mar ia
mar ia - avatar
0
Hug me 🤗🤗🤗
21st Oct 2020, 10:55 PM
Davide
Davide - avatar
0
Hhhhh😊
22nd Oct 2020, 7:23 AM
mar ia
mar ia - avatar