Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

تحديات برمجيةالتحدي الثالث - حل التمرين الثاني بلغة C

المطلوب

قم بتعريف دالة إسمها CountNoneEscapeChars, عند استدعاءها نمرر لها نص, فترجع عدد الأحرف الموجودة في هذا النص.
ملاحظة: أي حرف يعتبر Escape Character مثل الأحرف \t و \n إلخ.. لا يجب أن يتم حساب عددهم ضمن عدد الأحرف.
يمكنك إستخدام الـ Regex للتمييز بين الأحرف العادية و الأحرف التي تعتبر Escape Characters.

مثال: إذا قمنا باستخدام الدالة CountNoneEscapeChars() و تمرير النص "Hi Lora.\nHow are you?." فإنها سترجع الرقم 18.


الحل بلغة C

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int countNoneEscapeChars(char * s) {
if (s[0] == '\0')
{
return 0;
}
int counter = 0;
for (int i = 0; i < strlen(s); i++)
{
if (!isspace(s[i]))
{
counter++;
}
}
return counter;
}
void main() {
char * text = "Hi Lora.\nHow are you?.";
int numberOfNoneEscapeChars = countNoneEscapeChars(text);
printf("Total characters: %d", numberOfNoneEscapeChars );
}
#include <stdio.h> #include <string.h> #include <ctype.h> int countNoneEscapeChars(char * s) { if (s[0] == '\0') { return 0; } int counter = 0; for (int i = 0; i < strlen(s); i++) { if (!isspace(s[i])) { counter++; } } return counter; } void main() { char * text = "Hi Lora.\nHow are you?."; int numberOfNoneEscapeChars = countNoneEscapeChars(text); printf("Total characters: %d", numberOfNoneEscapeChars ); }

سنحصل على النتيجة التالية عند التشغيل.

Total characters: 18