อาร์เรย์ของอักขระเรียกว่าสตริง
ประกาศ
ต่อไปนี้เป็นการประกาศอาร์เรย์ -
char stringname [size];
ตัวอย่างเช่น − ถ่าน [50]; สตริงที่มีความยาว 50 ตัวอักษร
การเริ่มต้น
- การใช้ค่าคงที่อักขระตัวเดียว −
char a[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’} - การใช้ค่าคงที่สตริง −
char a[10] = "Hello":;
การเข้าถึง
มีสตริงควบคุม "%s" ที่ใช้สำหรับเข้าถึงสตริงจนกว่าจะพบ '\0'
ตรรกะที่ใช้ในการแปลงสระจากบนลงล่างหรือล่างขึ้นบน คือ −
for(i=0;string[i]!='\0';i++){
if(string[i]=='a'||string[i]=='e'||string[i]=='i'||string[i]=='o'||string[i]=='u'){
string[i]=toupper(string[i]);
}
}
printf("The result string with converted vowels is : ");
puts(string); โปรแกรม
ต่อไปนี้เป็นโปรแกรม C ที่ใช้ฟังก์ชันการแปลงเพื่อแปลงสตริงตัวพิมพ์ใหญ่เป็นสตริงตัวพิมพ์เล็ก -
#include<stdio.h>
#include<ctype.h>
void main(){
//Declaring variable for For loop (to read each position of alphabet) and string//
int i;
char string[40];
//Reading string//
printf("Enter the string : ");
gets(string);
//For loop to read each alphabet//
for(i=0;string[i]!='\0';i++){
if(string[i]=='a'||string[i]=='e'||string[i]=='i'||string[i]=='o'||string[i]=='u'){
string[i]=toupper(string[i]);
}
}
printf("The result string with converted vowels is : ");
puts(string);
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
Run 1: Enter the string : TutoRialsPoint The result string with converted vowels is : TUtORIAlsPOInt Run 2: Enter the string : c programming The result string with converted vowels is : c programming