الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

سؤال بلغة سي عن مصفوفة سلاسل

رائج
بدأه حامي الحمى في 15 يناير 2009 · 55 رد · 3,239 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#51

طيب حولت لك الكود على شكل دالة تستخدمه مباشرة.

الدالة تاخذ ثلاث متغيرات الاول المصفوفة التي بها النص الثاني الكلمه المطلوب البحث واستبدالها عنها الثالث الكلمه التي تريد ان تجعلها بدل االاصليه

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. void replace(char word[],char search[],char repl[])
  5. {
  6. int len = strlen(word);
  7. int s_len = strlen(search);
  8. int r_len = strlen(repl);
  9. char *current = word;
  10. while (current = strstr(current, search))
  11. {
  12. memmove(current + r_len , current + s_len, len - (current - word) - s_len + 1);
  13. strncpy(current, repl, r_len);
  14. }
  15.  
  16. }
  17. int main(int argc, char *argv[])
  18. {
  19.  
  20. char word[]="he is Mohmmad bin Omer";
  21. replace(word,"he","He");
  22. printf("%sn",word);
  23. replace(word,"is","Is");
  24. printf("%sn",word);
  25. replace(word,"Mohmmad","Khald");
  26. printf("%sn",word);
  27. replace(word,"bin","Abo");
  28. printf("%sn",word);
  29. getchar();
  30. return 0;
  31. }
  32.  
  33.  

tvquran_6.gif

#52

السلام عليكم

بارك الله فيك اخي الكريم

وجزاك الله عني كل خير

لكني عند محاولة استدعاء الدالة لم يطبع المطلوب

هذا الأمر الذي كتبته لاستدعاء الدالة

  1. replace(word,split,dic[choice]);
  2. printf("%sn",word);
  3.  
  4.  

هذا كود البرنامج كامل:

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define INPUT "stockWord.txt"
  5. #define L 12 // the length of each word
  6. #define W 50 //the maximum words on the dictionary
  7. #define M 100 // the maximum letters on the sentence that the user will enter it.
  8.  
  9.  
  10. int getWord(char dic[][L]);
  11. void sortarray(char dic[][L]);
  12. int PgetWord(char dic[][L]);
  13. void Psortarray(char dic[][L]);
  14. int CheckWord (char ch[]);
  15. void replace(char word[],char search[],char repl[]);
  16.  
  17.  
  18. void main()
  19. {
  20. char dic[W][L];
  21. char word [M];
  22. char *split;
  23. int i=0;
  24. int m=0;
  25. int choice;
  26. char answer;
  27. sortarray(dic);
  28. printf("enter sentence : ");
  29.  
  30.  
  31. gets(word);
  32. split=strtok(word," ");
  33.  
  34. while(split!=NULL)
  35. {
  36. if (!CheckWord(split))
  37. {
  38. printf("%s is not a word in our vocabulary, you may want to switch this word to the followin
    g substitutes:n",split);
  39. for(i=0;i<W;i++)
  40. {
  41. if(strncmp(split,dic[i],3)==0 && m++!=5)
  42. printf("%d. %sn",m,dic[i]);
  43. }
  44. m=0;
  45. printf("Would you like to switch this word?n");
  46. scanf("%c", &answer);
  47. if(answer=='N' || answer=='n')
  48. return;
  49. else
  50. if (answer=='Y' || answer=='y')
  51. {
  52. printf("To which substitute?n");
  53. scanf("%d", &choice);
  54. replace(word,split,dic[choice]);
  55. printf("%sn",word);
  56. }
  57. else
  58. {
  59. printf("take another choicen");
  60. scanf("%c",&answer);
  61. }
  62.  
  63. }
  64.  
  65. split=strtok(NULL," ");
  66.  
  67. }
  68.  
  69. getchar();
  70.  
  71. }
  72.  
  73.  
  74.  
  75.  
  76. //inputing the words from the text file and outputing the array.
  77. int getWord(char dic[][L])
  78. {
  79. FILE* file;
  80. int i,j;
  81. char ch;
  82. file=fopen(INPUT,"r");
  83.  
  84. for(i=0;i<W;i++)
  85. for(j=0;j<L;j++)
  86. dic[i][j]='\0';
  87. i=0;j=0;
  88. do
  89. {
  90. ch=fgetc(file);
  91. if(ch!='n')
  92. {
  93. dic[i][j]=ch;j++;
  94. }
  95. else if(i<W)
  96. {j=0;i++;}
  97.  
  98. }
  99. while(ch!=EOF);
  100.  
  101.  
  102. return 1;
  103.  
  104.  
  105. fclose(file);
  106.  
  107. }
  108.  
  109.  
  110. int CheckWord (char ch[])
  111. {
  112. int i;
  113. char dic[W][L];
  114. sortarray(dic);
  115. for(i=0;i<W;i++)
  116. if(strcmp(ch,dic[i])==0)
  117. return 1;
  118. return 0;
  119. }
  120.  
  121.  
  122.  
  123.  
  124. //sorting the array by the ABC letters.
  125. void sortarray(char dic[][L])
  126. {
  127. int i,j;
  128. char tmp[L];
  129. getWord(dic);
  130. for(i=0;i<W-1;i++)
  131. for(j=0;j<W;j++)
  132. if(strcmp(dic[i],dic[j])<0)
  133. {
  134. strcpy(tmp,dic[i]);
  135. strcpy(dic[i],dic[j]);
  136. strcpy(dic[j],tmp);
  137. }
  138. }
  139.  
  140.  
  141. void replace(char word[],char search[],char repl[])
  142. {
  143. int len = strlen(word);
  144. int s_len = strlen(search);
  145. int r_len = strlen(repl);
  146. char *current = word;
  147. while (current = strstr(current, search))
  148. {
  149. memmove(current + r_len , current + s_len, len - (current - word) - s_len + 1);
  150. strncpy(current, repl, r_len);
  151. }
  152. }
  153.  
  154.  
  155.  
  156.  
  157. //to print the array
  158. int PgetWord(char dic[][L])
  159. {
  160. FILE* file;
  161. int i,j;
  162. char ch;
  163. file=fopen(INPUT,"r");
  164.  
  165. for(i=0;i<W;i++)
  166. for(j=0;j<L;j++)
  167. dic[i][j]='\0';
  168. i=0;j=0;
  169. do
  170. {
  171. ch=fgetc(file);
  172. if(ch!='n')
  173. {
  174. dic[i][j]=ch;j++;
  175. }
  176. else if(i<W)
  177. {j=0;i++;}
  178.  
  179. }
  180. while(ch!=EOF);
  181. for(i=0;i<W;i++)
  182. {
  183. for(j=0;j<L;j++)
  184. {
  185. if(dic[i][j]=='\0')
  186. break;
  187. printf("%c",dic[i][j]);
  188. }
  189. printf("n");
  190. if(dic[i][0]=='\0') break;
  191. }
  192.  
  193. return 1;
  194.  
  195.  
  196. fclose(file);
  197.  
  198. }
  199.  
  200.  
  201.  
  202. // print the array after sorting
  203. void Psortarray(char dic[][L])
  204. {
  205. int i,j;
  206. char tmp[L];
  207. getWord(dic);
  208. for(i=0;i<W-1;i++)
  209. for(j=0;j<W;j++)
  210. if(strcmp(dic[i],dic[j])<0)
  211. {
  212. strcpy(tmp,dic[i]);
  213. strcpy(dic[i],dic[j]);
  214. strcpy(dic[j],tmp);
  215. }
  216.  
  217. for(i=0;i<W;i++)
  218. {
  219. if(dic[i][0]!='\0')
  220. printf("%sn",dic[i]);
  221. }
  222. }
  223.  
  224.  
  225.  

تم تعديل هذه المشاركة بواسطة حامي الحمى في 23 يناير 2009 في 21:15

تعب الجسد

ونعيم الروح

جسد يمشي فوق الأرض

وروح ترفرف حول العرش

#53

السلام عليكم

وينكم يا اخوان ؟

عارف اني ثقلتها بس الله يعينكم، هذا اخر قسم من السؤال

بارك الله فيكم سلف

تعب الجسد

ونعيم الروح

جسد يمشي فوق الأرض

وروح ترفرف حول العرش

#54

السلام عليكم

يا اخوان ما في جد يساعد ؟

انا عملت عدة تجارب

لاقيت في النهاية ان المشكلة في الـ split

لاني جربت اغير الـ split في كلمة والدالة قامت بالقراءة بشكل صحيح

لكن الظاهر اني لازم اضع شيء ثاني غير الـ split

يا ريت حد يرشدني

بارك الله فيكم

السلام عليكم

تعب الجسد

ونعيم الروح

جسد يمشي فوق الأرض

وروح ترفرف حول العرش

#55

المشكلة فقط ان word تتغير والرقم المسند ليس هو الصحيح:

هذا تصحيح:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define INPUT "stockWord.txt"
  4. #define L 12 // the length of each word
  5. #define W 50 //the maximum words on the dictionary
  6. #define M 100 // the maximum letters on the sentence that the user will enter it.
  7.  
  8.  
  9. int getWord(char dic[][L]);
  10. void sortarray(char dic[][L]);
  11. int PgetWord(char dic[][L]);
  12. void Psortarray(char dic[][L]);
  13. int CheckWord (char ch[]);
  14. void replace(char word[],char* search,char repl[]);
  15.  
  16.  
  17. void main()
  18. {
  19. char dic[W][L];
  20. char word [M];
  21. char TMP[M];
  22. char *split;
  23. int yeh=1;
  24. int u;
  25. int i=0;
  26. int m=0;
  27. int choice;
  28. char answer;
  29. sortarray(dic);
  30. printf("enter sentence : ");
  31.  
  32.  
  33. gets(word);
  34. strcpy(TMP,word);
  35. split=strtok(word," ");
  36.  
  37. while(split!=NULL)
  38. {
  39. if (!CheckWord(split))
  40. {
  41. printf("%s is not a word in our vocabulary, you may want to switch this word to the followin
    g substitutes:n",split);
  42. for(i=0;i<W;i++)
  43. {
  44. if(strncmp(split,dic[i],3)==0 && m++!=5)
  45. {
  46. if(yeh){u=i;yeh=0;}
  47. printf("%d. %sn",m,dic[i]);
  48. }
  49. }
  50. m=0;
  51. printf("Would you like to switch this word?n");
  52. scanf("%c", &answer);
  53. if(answer=='N' || answer=='n')
  54. return;
  55. else
  56. if (answer=='Y' || answer=='y')
  57. {
  58. printf("To which substitute?n");
  59. scanf("%d", &choice);
  60. printf("%sn",word);
  61.  
  62. replace(TMP,split,dic[choice+u-1]);
  63. printf("%sn",TMP);
  64. }
  65. else
  66. {
  67. printf("take another choicen");
  68. scanf("%c",&answer);
  69. }
  70.  
  71. }
  72.  
  73. split=strtok(NULL," ");
  74.  
  75. }
  76.  
  77. getchar();
  78. getchar();
  79.  
  80. }
  81.  
  82.  
  83.  
  84.  
  85. //inputing the words from the text file and outputing the array.
  86. int getWord(char dic[][L])
  87. {
  88. FILE* file;
  89. int i,j;
  90. char ch;
  91. file=fopen(INPUT,"r");
  92.  
  93. for(i=0;i<W;i++)
  94. for(j=0;j<L;j++)
  95. dic[i][j]='\0';
  96. i=0;j=0;
  97. do
  98. {
  99. ch=fgetc(file);
  100. if(ch!='n')
  101. {
  102. dic[i][j]=ch;j++;
  103. }
  104. else if(i<W)
  105. {j=0;i++;}
  106.  
  107. }
  108. while(ch!=EOF);
  109.  
  110.  
  111. return 1;
  112.  
  113.  
  114. fclose(file);
  115.  
  116. }
  117.  
  118.  
  119. int CheckWord (char ch[])
  120. {
  121. int i;
  122. char dic[W][L];
  123. sortarray(dic);
  124. for(i=0;i<W;i++)
  125. if(strcmp(ch,dic[i])==0)
  126. return 1;
  127. return 0;
  128. }
  129.  
  130.  
  131.  
  132.  
  133. //sorting the ãÕÝæÝÉ',CAPTION,'Array',WRAP, TEXTFONTCLASS,'myText
    ','purple');" onmouseout=nd(); href="">Array by the ABC letters.
  134. void sortarray(char dic[][L])
  135. {
  136. int i,j;
  137. char tmp[L];
  138. getWord(dic);
  139. for(i=0;i<W;i++)
  140. for(j=0;j<W;j++)
  141. if(dic[i][0]!=' '&&dic[j][0]!=0)
  142. if(strcmp(dic[i],dic[j])<0)
  143. {
  144. strcpy(tmp,dic[i]);
  145. strcpy(dic[i],dic[j]);
  146. strcpy(dic[j],tmp);
  147. }
  148. }
  149.  
  150.  
  151. void replace(char word[],char* search,char repl[])
  152. {
  153. int len = strlen(word);
  154. int s_len = strlen(search);
  155. int r_len = strlen(repl);
  156. char *current = word;
  157. while (current = strstr(current, search))
  158. {
  159. memmove(current + r_len , current + s_len, len - (current - word) - s_len + 1);
  160. strncpy(current, repl, r_len);
  161. }
  162. }
  163.  
  164.  
  165.  
  166.  
  167. //to print the array
  168. int PgetWord(char dic[][L])
  169. {
  170. FILE* file;
  171. int i,j;
  172. char ch;
  173. file=fopen(INPUT,"r");
  174.  
  175. for(i=0;i<W;i++)
  176. for(j=0;j<L;j++)
  177. dic[i][j]='\0';
  178. i=0;j=0;
  179. do
  180. {
  181. ch=fgetc(file);
  182. if(ch!='n')
  183. {
  184. dic[i][j]=ch;j++;
  185. }
  186. else if(i<W)
  187. {j=0;i++;}
  188.  
  189. }
  190. while(ch!=EOF);
  191. for(i=0;i<W;i++)
  192. {
  193. for(j=0;j<L;j++)
  194. {
  195. if(dic[i][j]=='\0')
  196. break;
  197. printf("%c",dic[i][j]);
  198. }
  199. printf("n");
  200. if(dic[i][0]=='\0') break;
  201. }
  202.  
  203. return 1;
  204.  
  205.  
  206. fclose(file);
  207.  
  208. }
  209.  
  210.  
  211.  
  212. // print the ãÕÝæÝÉ',CAPTION,'Array',WRAP, TEXTFONTCLASS,'myText'
    ,'purple');" onmouseout=nd(); href="">Array after sorting
  213. void Psortarray(char dic[][L])
  214. {
  215. int i,j;
  216. char tmp[L];
  217. getWord(dic);
  218. for(i=0;i<W-1;i++)
  219. for(j=0;j<W;j++)
  220. if(strcmp(dic[i],dic[j])<0)
  221. {
  222. strcpy(tmp,dic[i]);
  223. strcpy(dic[i],dic[j]);
  224. strcpy(dic[j],tmp);
  225. }
  226.  
  227. for(i=0;i<W;i++)
  228. {
  229. if(dic[i][0]!='\0')
  230. printf("%sn",dic[i]);
  231. }
  232. }
  233.  
  234.  
  235.  
  236.  

tvquran_6.gif

#56

السلام عليكم

بارك الله فيك اخي الكريم

ربنا يفتح عليك فتوح العارفين

تعب الجسد

ونعيم الروح

جسد يمشي فوق الأرض

وروح ترفرف حول العرش

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…