C語言練習題:字串(C language exercise: String)
練習一:字串輸入
撰寫一可以輸入字串並輸出所輸入字串的程式。
Exercise 1: Input a string
Design a program to input a string and displays it.
練習一參考解法:
Exercise 1 solution:
/*
Input a string and displays it.
Author: Holan
*/#include <stdio.h>
#include <stdlib.h>int main()
{
const int SIZE = 100;
char str[SIZE]; printf("Input a string:");
// Get any string with space method 1
scanf("%[^\n]%*c", str);
printf("Your string: %s\n", str); printf("Input a string:");
// Get any string with space method 2
gets(str);
printf("Your string: %s\n", str); printf("Input a string:");
// Get any string with space method 3
fgets(str, SIZE, stdin);
printf("Your string: %s\n", str); return 0;
}
練習二:字串長度
撰寫一可以算出字串長度的程式,請不要使用strlen()函數。
Exercise 2: String’s Length
Write a program to find the length of a string without using strlen() function.
練習二參考解法:
Exercise 2 solution:
/*
Find the length of a string without using strlen() function.
Author: Holan
*/#include <stdio.h>
#include <stdlib.h>int main()
{
const int SIZE = 100;
char str[SIZE], cnt = 0; printf("Input a string:"); // Get any string with space method 1
//scanf("%[^\n]%*c", str); // Get any string with space method 2
//gets(str); // Get any string with space method 3
fgets(str, SIZE, stdin); while(str[cnt] != '\0' && ++cnt < SIZE); printf("Length:%d\n", cnt); return 0;
}
練習三:字元
設計可以將一字串的每個字元以空白區隔做輸出。
Exercise 3: Characters
Design a program to split a string into characters.
練習三參考解法:
Exercise 3 solution:
/*
Split a string into characters.
Author: Holan
*/#include <stdio.h>
#include <stdlib.h>int main()
{
const int SIZE = 100;
char str[SIZE], idx = 0;; printf("Input a string:"); gets(str); while(str[idx] != '\0')
printf("%c ", str[idx++]); return 0;
}
練習四:大小寫轉換
設計可以將字串的小寫字母轉換成大寫字母,大寫字母轉換成小寫字母。例如 HelLoWoRLD 經轉換後,變成hELlOwOrld。
Exercise 4: Invert Case
Design a program that inverts string case. For example: HelLoWoRLD will be inverted to hELlOwOrld.
練習四參考解法:
Exercise 4 solution:
/*
Invert Case
Author: Holan
*/#include <stdio.h>
#include <stdlib.h>int main()
{
const int SIZE = 100;
char str[SIZE], idx = 0;; printf("Input a string:"); gets(str); while(str[idx] != '\0')
{
char c = str[idx]; // Convert to upper case.
if(c >= 'a' && c <= 'z')
c = c - 32;
// Convert to lower case.
else if(c >= 'A' && c <= 'Z')
c = c + 32; printf("%c", c);
idx++;
} return 0;
}
練習五:反轉字串
設計可以將字串反轉的程式,例如 Hello World 會變成 dlroW olleH。
Exercise 5: Reverse String
Design a program to reverse any string. For example: Hello World ==> dlroW olleH
練習五參考解法:
Exercise 5 solution:
/*
Reversed string
Author: Holan
*/#include <stdio.h>
#include <stdlib.h>int main()
{
const int SIZE = 100;
char str[SIZE], idx = 0;; printf("Input a string:"); gets(str); while(str[idx] != '\0')
idx++; while(idx >= 0)
printf("%c", str[--idx]);
return 0;
}
練習六:母音或子音
設計一個可以算出字串中的母音字母的個數與子音字母的個數。
Exercise 6: Vowel or consonant
Design a program that finds the total of vowel or consonant in a string.
練習六參考解法:
Exercise 6 solution:
/*
Vowel or consonant
Author: Holan
*/#include <stdio.h>
#include <stdlib.h>int main()
{
const int SIZE = 100;
char str[SIZE], idx = 0;
int vowelCnt = 0, consonantCnt = 0; printf("Input a string:"); gets(str); while(str[idx] != '\0')
{
char c = str[idx];
switch(c)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
vowelCnt++;
break; default:
if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
consonantCnt++;
break;
}
idx++;
} printf("Total vowel:%d\tTotal consonant:%d\n", vowelCnt, consonantCnt);
return 0;
}
練習七:迴文
設計可以判斷迴文的程式。例如:madam 是迴文。
Exercise 7: Palindrome
Design a program that checks whether a string is palindrome or not.
練習七參考解法:
Exercise 7 solution:
/*
Palindrome
Author: Holan
*/#include <stdio.h>
#include <stdlib.h>int main()
{
const int SIZE = 100;
char str[SIZE], idx = 0;
int isPalindrome = 1; printf("Input a string:"); gets(str); while(str[idx] != '\0')
idx++; int i = 0;
while(i < idx)
{
if(str[i++] != str[--idx])
{
isPalindrome = 0;
break;
}
} if(isPalindrome != 0)
printf("Yes\n");
else
printf("No\n"); return 0;
}
練習八:出現次數
設計一個可以統計每個字元在字串中出現的次數。
Exercise 8: Character Frequency
Design a program that calculates each character’s frequency in a string.
練習八參考解法:
Exercise 8 solution:
/*
Character Frequency
Author: Holan
*/#include <stdio.h>
#include <stdlib.h>int main()
{
const int CHR_NO = 255;
const int SIZE = 1000;
char str[SIZE];
int frequency[CHR_NO]; for(int i = 0; i < CHR_NO; i++)
frequency[i] = 0; printf("Input a string:"); gets(str); for(int i = 0; str[i] != '\0'; i++)
{
frequency[(int)(str[i])]++;
} for(int i = 0; i < CHR_NO; i++)
{
printf("%c:%d\n", i, frequency[i]);
} return 0;
}
練習九:出現次數最高的字元
設計一個可以統計每個字元在字串中出現的次數,並找次數最高的字元與次數。
Exercise 9: Highest frequency character
Design a program to find the highest frequency character in a given string.
練習九參考解法:
Exercise 9 solution:
/*
Highest Character Frequency
Author: Holan
*/#include <stdio.h>
#include <stdlib.h>int main()
{
const int CHR_NO = 255;
const int SIZE = 1000;
char str[SIZE];
int frequency[CHR_NO];
int maxFreq = 0;
char mChr; for(int i = 0; i < CHR_NO; i++)
frequency[i] = 0; printf("Input a string:"); gets(str); for(int i = 0; str[i] != '\0'; i++)
{
frequency[(int)(str[i])]++;
} for(int i = 0; i < CHR_NO; i++)
{
if(frequency[i] > maxFreq)
{
maxFreq = frequency[i];
mChr = (char)(i);
}
} printf("%c %d\n", mChr, maxFreq);
return 0;
}
練習十:取代空白字元
設計可以將字串中的空白字元以特定的字元取代。
Exercise 10: Spaces replacing
Design a program that replaces all spaces in a given with a specific character.
練習十參考解法:
Exercise 10 solution:
/*
Spaces replacing
Author: Holan
*/#include <stdio.h>
#include <stdlib.h>int main()
{
const int SIZE = 1000;
char str[SIZE];
char aChr;
printf("Input a string:");
gets(str); printf("Enter a special character:");
scanf("%c", &aChr); for(int i = 0; str[i] != '\0'; i++)
{
if(str[i] == ' ') str[i] = aChr;
}
printf("The input string becomes %s\n", str);
return 0;
}
Originally published at https://yunlinsong.blogspot.com.