ตัวอย่างโค้ดภาษาซี ในการเขียนเพื่อวนรับค่าตัวอักษร จากนั้นนำค่าที่ได้มาทำการเปลี่ยนตัวอักษรจากตัวเล็กให้เป็นตัวใหญ่ ตัวใหญ่ให้เป็นตัวเล็ก ส่วนตัวเลขหรืออักขระอื่นๆ ยังคงแสดงผลเหมือนเดิม
ตัวอย่าง Flowchart
–
ตัวอย่างโค้ด แบบที่ 1
/***************************************************
* Author : CS Developers
* Author URI: https://www.comscidev.com
* Facebook : https://www.facebook.com/CSDevelopers
***************************************************/
#include <stdio.h>
int main()
{
char str[100];
int i;
while(1)
{
printf("\n Enter char : ");
scanf("%s", &str);
i=0;
while(str[i] != '\0')
{
if(str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] + 32;
}
else if(str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32;
}
i++;
}
printf(" Convert char : %s\n", str);
}
return 0;
}
ตัวอย่างโค้ด แบบที่ 2
/***************************************************
* Author : CS Developers
* Author URI: https://www.comscidev.com
* Facebook : https://www.facebook.com/CSDevelopers
***************************************************/
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int i;
while(1)
{
printf("\n Enter char : ");
scanf("%s", &str);
i=0;
while(str[i] != '\0')
{
if(isupper(str[i]))
{
str[i] = tolower(str[i]);
}
else if(islower(str[i]))
{
str[i] = toupper(str[i]);
}
i++;
}
printf(" Convert char : %s\n", str);
}
return 0;
}
แสดงผล
