עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

איך אפשר למצוא גודל של מחרוזת הכוללת רווחים בשפת C?

Featured Replies

פורסם

היי חברים!

יש לי תכנית שאני צריך לבנות.

איך אני יכול למצוא מה גודל המחרוזת הכולל רווחים?

הפונקציה

strlen(str)

(כאשר str זוהי שם של סתם מחרוזת) יודעת להחזיר את גודל התווים עד לרווח הראשון. כלומר היא לא יודעת מה אורך כל המשפט אלא רק מה אורך המילה הראשונה.

איך אני מוצא את אורך כל המשפט?

תודה רבה!

פורסם

strlen סופרת את כל אורך המחרוזת כולל רווחים עד התו null .

פורסם
  • מחבר

strlen סופרת את כל אורך המחרוזת כולל רווחים עד התו null .

אם זה היה כך חיי היו הרבה יותר פשוטים.

זה תופס עד לרווח הראשון. דוגמה:

hello world

strlen יחזיר שאורך המחרוזת היא 5 כאורך המילה hello.

פורסם

אתה בטוח שאתה עובד בשפת C?

תהדר ותריץ את הקוד הבא


#include <string.h>
#include <stdio.h>
int main (){
int a;
a=strlen("hello world");
printf ("%i\n" ,a);
}

פורסם

אם זה היה כך חיי היו הרבה יותר פשוטים.

זה תופס עד לרווח הראשון. דוגמה:

hello world

strlen יחזיר שאורך המחרוזת היא 5 כאורך המילה hello.

זה פשוט לא נכון.

אתה בטוח שלא עשית משהו כזה?

char str[100];
scanf("%s", str);
printf("%d", strlen(str));

אם כן, אז מה שקרא הוא שהמחרוזת נקראה רק עד הרווח הראשון, כלומר אם המשתמש הכניס "hello world" אז ה-scanf תקרא רק את ה-hello.

פורסם

^

צודק.

תקלוט מחרוזת בעזרת הפונקציה gets

פורסם

אופס כנראה שלא הבנתי כתבתי פונקציה שמדפיסה אורך של כל מילה :kopfpatsch:


#include <stdio.h>
#include <string.h>
char * ptr;
char * lastptr;
char str[]="hello world my name is niseg"
lastptr=str;
while ((ptr=strtok(str,' ')))
{
printf("%d \n ", ((unsigned long)ptr)-(unsigned long)lastptr) );
lastptr=ptr;
}

תבדוק כאן http://www.cplusplus.com/reference/clibrary/cstring/strtok/

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.