fork download
  1. # include <stdio.h>
  2.  
  3. int isPalindrome(char s[]){
  4. //関数の中だけを書き換えてください
  5. //回文になっているとき1を返す
  6. int isPalindrome(char s[]) {
  7. int len = strlen(s);
  8. int i, j;
  9.  
  10. // 文字列の先頭と末尾から順に比較していきます
  11. for (i = 0, j = len - 1; i < j; i++, j--) {
  12. if (s[i] != s[j]) {
  13. return 0; // 文字が一致しない場合、回文ではありません
  14. }
  15. }
  16.  
  17. return 1; // 文字がすべて一致した場合、回文です
  18. }
  19.  
  20. int main() {
  21. char s[100];
  22.  
  23. printf("文字列を入力してください: ");
  24. scanf("%s", s);
  25.  
  26. if (isPalindrome(s)) {
  27. printf("回文です\n");
  28. } else {
  29. printf("回文ではありません\n");
  30. }
  31.  
  32. return 0;
  33. }
  34. //回文になっていないとき0を返す
  35. }
  36.  
  37. //メイン関数は書き換えなくてよいです
  38. int main(){
  39. char s[100];
  40. scanf("%s",s);
  41. printf("%s -> %d\n",s,isPalindrome(s));
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0s 5292KB
stdin
gig
stdout
gig -> 0