‏إظهار الرسائل ذات التسميات لغة سي c. إظهار كافة الرسائل
‏إظهار الرسائل ذات التسميات لغة سي c. إظهار كافة الرسائل

C Program to reverse a line برنامج C لعكس سطر

 C Program to reverse a line برنامج C لعكس سطر
C Program to reverse a line برنامج C لعكس سطر


التنفيذ

الآن ، سنرى التنفيذ الفعلي للبرنامج -

#include <stdio.h>
#include <string.h>

int string_length(char s[]) {
   int i = 0;

   while(s[i]!='\0')
      i++;

   return i;	
}

void string_reverse(char st[]) {
   int i,j,len;
   char ch;

   j = len = string_length(st) - 1;
   i = 0;

   while(i < j) {
      ch = st[j];
      st[j] = st[i];
      st[i] = ch;
      i++;
      j--;
   }
}

int main (void) {
   char line[] = "Taj Mahal is one of the seven wonders of the world";
   char reverse[100] = "", temp[50];
   int i, j, n;

   n = string_length(line);

   for(i = n-1; i >= 0; --i) {

      for(j = 0; i >= 0 && line[i] != ' '; --i,++j)
         temp[j] = line[i];

      temp[j] = '\0';

      string_reverse(temp);
      
      strcat(reverse,temp);
      strcat(reverse," ");
   }

   printf("Original - %s\n", line);
   printf("Reversed - %s\n",reverse);

   return 0;
}

المخرجات 

يجب أن يكون ناتج هذا البرنامج -

Original - Taj Mahal is one of the seven wonders of the world
Reversed - world the of wonders seven the of one is Mahal Taj


C Program to reverse words in a line برنامج C لعكس الكلمات في سطر

 C Program to reverse words in a line برنامج C لعكس الكلمات في سطر

C Program to reverse words in a line برنامج C لعكس الكلمات في سطر

التنفيذ

الآن ، سنرى التنفيذ الفعلي للبرنامج -

#include <stdio.h>
#include <string.h>

int string_length(char s[]) {
   int i = 0;

   while(s[i]!='\0')
      i++;

   return i;	
}

void string_reverse(char st[]) {
   int i,j,len;
   char ch;

   j = len = string_length(st) - 1;
   i = 0;

   while(i < j) {
      ch = st[j];
      st[j] = st[i];
      st[i] = ch;
      i++;
      j--;
   }
}

int main (void) {
   char line[] = "Taj Mahal is one of the seven wonders of the world";
   char reverse[100]="",temp[50];
   int i,j,n;

   n = string_length(line);

   for(i = 0; i < n; i++) {

      for(j = 0; i < n && line[i]!=' '; ++i,++j) {
         temp[j] = line[i];
      }
      
      temp[j] = '\0';

      string_reverse(temp);

      strcat(reverse, temp);
      strcat(reverse, " ");
   }
   
   printf("Original - %s\n", line);
   printf("Reversed - %s\n",reverse);
   
   return 0;
}

المخرجات 

يجب أن يكون ناتج هذا البرنامج -

Original - Taj Mahal is one of the seven wonders of the world
Reversed - jaT lahaM si eno fo eht neves srednow fo eht dlrow





String Anagram Program in C برنامج String Anagram في C

 String Anagram Program in C برنامج String Anagram في C

String Anagram Program in C برنامج String Anagram في C

التنفيذ

الآن ، سنرى التنفيذ الفعلي للبرنامج -

#include <stdio.h>
#include <string.h>

int main (void) {
   char s1[] = "recitals";
   char s2[] = "articles";

   char temp;

   int i, j;
   int n  = strlen(s1);
   int n1 = strlen(s2);

   // If both strings are of different length, then they are not anagrams

   if( n != n1) {    
      printf("%s and %s are not anagrams! \n", s1, s2);
      return 0;
   }
   
   // lets sort both strings first −

   for (i = 0; i < n-1; i++) {
      for (j = i+1; j < n; j++) {
         if (s1[i] > s1[j]) {
            temp  = s1[i];
            s1[i] = s1[j];
            s1[j] = temp;
         }
         if (s2[i] > s2[j]) {
            temp  = s2[i];
            s2[i] = s2[j];
            s2[j] = temp;
         }
      }
   }

   // Compare both strings character by character

   for(i = 0; i<n; i++) {
      if(s1[i] != s2[i]) {    
         printf("Strings are not anagrams! \n", s1, s2);
         return 0;
      }
   }

   printf("Strings are anagrams! \n");
   return 0;
}

المخرجات 

يجب أن يكون ناتج هذا البرنامج -

Strings are anagrams!




Program to Concatenate Strings in C برنامج لتسلسل السلاسل في C

 Program to Concatenate Strings in C برنامج لتسلسل السلاسل في C

Program to Concatenate Strings in C برنامج لتسلسل السلاسل في C


التنفيذ

الآن ، سنرى التنفيذ الفعلي للبرنامج -

#include <stdio.h>
#include <string.h>

int main() {
   char s1[10] = "Taj";
   char s2[] = "Mahal";

   int i, j, n1, n2;

   n1 = strlen(s1);
   n2 = strlen(s2);

   j = 0;
   for(i = n1; i< n1+n2; i++ ) {
      s1[i] = s2[j];
      j++;
   }

   s1[i] = '\0';

   printf("%s", s1);

   return 0;
}

المخرجات 

يجب أن يكون ناتج هذا البرنامج -

TajMahal




Program to Compare Strings in C برنامج لمقارنة السلاسل في لغة سي

 Program to Compare Strings in C برنامج لمقارنة السلاسل في لغة سي

Program to Compare Strings in C برنامج لمقارنة السلاسل في لغة سي

التنفيذ

الآن ، سنرى التنفيذ الفعلي للبرنامج -

#include <stdio.h>

int main() {
   char s1[] = "advise";     
   char s2[] = "advice";
    
   int n = 0;
   unsigned short flag = 1; 
    
   while (s1[n] != '\0') {
      if(s1[n] != s2[n]) {
         flag = 0;
         break;
      }
      n++;
   }
    
   if(flag == 1) {
      printf("%s and %s are identical\n", s1, s2);
   } else {
      printf("%s and %s are NOT identical\n", s1, s2);
   }

   return 0;
}

المخرجات 

يجب أن يكون ناتج هذا البرنامج -

advise and advice are NOT identical




Program to Swap Strings in C برنامج لمبادلة النصوص في C

 Program to Swap Strings in C برنامج لمبادلة النصوص في C

Program to Swap Strings in C برنامج لمبادلة النصوص في C

التنفيذ

الآن ، سنرى التنفيذ الفعلي للبرنامج -

#include <stdio.h>

int main() {
   char s1[] = "TajMahal";     
   char s2[] = "Dazzling";     
   char ch;

   int index = 0;

   //Character by Character approach

   printf("Before Swapping - \n");
   printf("Value of s1 - %s \n", s1);
   printf("Value of s2 - %s \n", s2);

   while(s1[index] != '\0') {
      ch = s1[index];
      s1[index] = s2[index];
      s2[index] = ch;
      index++;
   }

   printf("After Swapping - \n");
   printf("Value of s1 - %s \n", s1);
   printf("Value of s2 - %s \n", s2);

   return 0;
}

المخرجات 

يجب أن يكون ناتج هذا البرنامج -

Before Swapping -
Value of s1 - TajMahal
Value of s2 - Dazzling
After Swapping -
Value of s1 - Dazzling
Value of s2 - TajMahal




Program to Search Strings in C برنامج لسلاسل البحث في C

 Program to Search Strings in C برنامج لسلاسل البحث في C



Program to Search Strings in C برنامج لسلاسل البحث في C
Program to Search Strings in C برنامج لسلاسل البحث في C


التنفيذ

الآن ، سنرى التنفيذ الفعلي للبرنامج -

#include <stdio.h>
#include <string.h>

int main() {
   char s1[] = "Beauty is in the eye of the beholder";
   char s2[] = "the";

   int n = 0;
   int m = 0;
   int times = 0;
   int len = strlen(s2);      // contains the length of search string

   while(s1[n] != '\0') {

      if(s1[n] == s2[m]) {     // if first character of search string matches

         // keep on searching

         while(s1[n] == s2[m]  && s1[n] !='\0') {
            n++;
            m++;
         }

         // if we sequence of characters matching with the length of searched string
         if(m == len && (s1[n] == ' ' || s1[n] == '\0')) {

            // BINGO!! we find our search string.
            times++;
         }
      } else {            // if first character of search string DOES NOT match
         while(s1[n] != ' ') {        // Skip to next word
            n++;
            if(s1[n] == '\0')
            break;
         }
      }
		
      n++;
      m=0;  // reset the counter to start from first character of the search string.
   }

   if(times > 0) {
      printf("'%s' appears %d time(s)\n", s2, times);
   } else {
      printf("'%s' does not appear in the sentence.\n", s2);
   }

   return 0;
}

المخرجات 

يجب أن يكون ناتج هذا البرنامج -

'the' appears 2 time(s)