Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #define STRING_LENGTH 64
  3.  
  4. int isalphaCustom(char character)
  5. {
  6. if (character >= 'A' && character <= 'Z' ||
  7. character >= 'a' && character <= 'z' ||
  8. character == '\x86' ||
  9. character == '\x84' ||
  10. character == '\x94' ||
  11. character == '\x8F' ||
  12. character == '\x8E' ||
  13. character == '\x99')
  14. return 1;
  15. return 0;
  16. }
  17.  
  18. char *getWord(char string[], int word)
  19. {
  20. if (word < 0)
  21. return NULL;
  22.  
  23. int currentWord = 0;
  24. for (int i = 0; string[i] != '\0'; i++)//Loop till slutet av strängen, bryts när rätt ord hittats
  25. {
  26. if (isalphaCustom(string[i]) && !isalphaCustom(string[i - 1]))//Första bokstaven efter specialtecken
  27. {
  28. if (currentWord == word)//Hittat rätt ord
  29. return &string[i];
  30. currentWord++;
  31. }
  32. }
  33. return NULL;
  34. }
  35.  
  36. void printFirstWord(char string[])
  37. {
  38. int i = 0;
  39. while (isalphaCustom(string[i]))//Skriver tills specialtecken eller mellanslag hittas
  40. {
  41. printf("%c", string[i]);
  42. i++;
  43. }
  44. printf("\n");
  45. }
  46.  
  47. void printWord(char string[], int word)
  48. {
  49. char *pWord = getWord(string, word);
  50. if (pWord != NULL)
  51. {
  52. printf("Ord %d i \"%s\" \x84r: ", word, string);
  53. printFirstWord(pWord);
  54. }
  55. else
  56. printf("Ordet finns ej\n");
  57. }
  58.  
  59.  
  60. int main(void)
  61. {
  62. int word;
  63. char string[STRING_LENGTH];
  64. char repeat;
  65.  
  66. do
  67. {
  68. printf("Skriv en mening: ");
  69. fgets(string, STRING_LENGTH, stdin);
  70. for (int i = 0; i < STRING_LENGTH; i++)//Tar bort radbyte från input, underlättar utskrift
  71. if (string[i] == '\n')
  72. string[i] = '\0';
  73.  
  74. printf("Vilket ord vill du skriva ut? ");
  75. scanf_s("%d", &word);
  76. printWord(string, word);
  77.  
  78. printf("igen? y/n");
  79. scanf_s(" %c", &repeat);
  80.  
  81. char c;
  82. while ((c = getchar()) != '\n' && c != EOF);//Flush stdin
  83. } while (repeat == 'y');
  84.  
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement