2
0

Beginners_-_String_functions.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "raylib.h"
  2. // string.h
  3. // for strlen()=length of string, strcmp()=compare two strings,
  4. // strlwr()=convert string to lower case, strupr()=convert string to upper case.
  5. #include "string.h"
  6. int main(void)
  7. {
  8. // Initialization
  9. //--------------------------------------------------------------------------------------
  10. const int screenWidth = 800;
  11. const int screenHeight = 450;
  12. InitWindow(screenWidth, screenHeight, "raylib example.");
  13. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  14. //--------------------------------------------------------------------------------------
  15. // Main game loop
  16. while (!WindowShouldClose()) // Detect window close button or ESC key
  17. {
  18. // Update
  19. //----------------------------------------------------------------------------------
  20. char lenny[10] = "hello";
  21. char com1[] = "abc";
  22. char com2[] = "abc";
  23. char com3[] = "abcd";
  24. char lower[] = "IsThisLow?";
  25. char higher[] = "IsThisHigh?";
  26. //----------------------------------------------------------------------------------
  27. // Draw
  28. //----------------------------------------------------------------------------------
  29. BeginDrawing();
  30. ClearBackground(RAYWHITE);
  31. DrawText(FormatText("Length of lenny is : %i",strlen(lenny)),100,200,20,DARKGRAY);
  32. // strlen returns the amount of characters in the char.
  33. if(strcmp(com1,com2)==0){
  34. DrawText("chars com1 and com2 have same contents.",100,220,20,DARKGRAY);
  35. }else{
  36. DrawText("chars com1 and com2 do not have same contents.",100,220,20,DARKGRAY);
  37. }
  38. if(strcmp(com1,com3)==0){
  39. DrawText("chars com1 and com2 have same contents.",100,240,20,DARKGRAY);
  40. }else{
  41. DrawText("chars com1 and com2 do not have same contents.",100,240,20,DARKGRAY);
  42. }
  43. // with strcmp it returns <0 or >0 if the char is different. ==0 if the same.
  44. DrawText(FormatText("char lower in lower case is : %s",strlwr(lower)),100,260,20,DARKGRAY);
  45. DrawText(FormatText("char higher in higher case is : %s",strupr(higher)),100,280,20,DARKGRAY);
  46. EndDrawing();
  47. //----------------------------------------------------------------------------------
  48. }
  49. // De-Initialization
  50. //--------------------------------------------------------------------------------------
  51. CloseWindow(); // Close window and OpenGL context
  52. //--------------------------------------------------------------------------------------
  53. return 0;
  54. }