C program remove spaces, blanks from a string

  • برمجة سي c

C program remove spaces, blanks from a string

 

C program to remove spaces or excess blanks from a string, For example, consider the string

"C  programming"

There are two spaces in this string, so our program will print the string
"C programming." It will remove spaces when they occur more than one time consecutively in string anywhere.

الأجوبة

#include <stdio.h>
 
int main()
{
   char text[1000], blank[1000];
   int c = 0, d = 0;
 
   printf("Enter some text\n");
   gets(text);
 
   while (text[c] != '\0') {
      if (text[c] == ' ') {
         int temp = c + 1;
         if (text[temp] != '\0') {
            while (text[temp] == ' ' && text[temp] != '\0') {
               if (text[temp] == ' ') {
                  c++;
               }  
               temp++;
            }
         }
      }
      blank[d] = text[c];
      c++;
      d++;
   }
 
   blank[d] = '\0';
 
   printf("Text after removing blanks\n%s\n", blank);
 
   return 0;
}
هل كان المحتوى مفيد؟

القوائم الدراسية التي ينتمي لها السؤال

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...