1. 程式人生 > >C語言學習小問題總結!!!

C語言學習小問題總結!!!

字串轉整型

#include <stdlib.h>
...
char cs[] = "123";
int num = atoi(cs);

分隔字串

#include<string.h>
...
char str[] = "I am student";
char *ptr;  
ptr = strtok(str, " "); 
while (ptr != NULL) {  
     print("%s",ptr);
     //輸出每一個字元/字串
     ptr = strtok(NULL, " ");  
}  

控制檯確定輸入整數個數,輸入整數後統計奇數和偶數的個數

int n ,num,odd=0,even=0;
    scanf("%d",&n);
    for(int i =0;i<n;i++){
        scanf("%d",&num);
        if((num%2)!=0) {
                ++odd;
            }
            else {
                ++even;
            }
    }
    printf("%d %d",odd,even);