我的代碼可以洗掉任何元音并將單詞的第一個字母列印為大寫字母。我怎樣才能讓我的預期輸出作業?如果值為“ I am Iron Man”(帶有前導空格),它會作業并列印“ M Rn Mn”。但是,如果字串開頭沒有空格,我的輸出是“ m Rn Mn”,但預期的輸出是“ M Rn Mn”。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char str[] = "I am Iron Man";
int i, j, len = 0;
len = strlen(str);
// Accepting input.
for (i = 0; i < len; i ) {
// Check vowels.
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
// delete vowel syntax
for (j = i; j < len; j ) {
// Store after removing vowels
str[j] = str[j 1];
}
i--;
len--;
}
str[len 1] = '\0';
}
for(i=0; str[i]!='\0'; i )
{
//check first character is lowercase alphabet
if(i==0)
{
if((str[i]>='a' && str[i]<='z'))
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}
if(str[i]==' ')//check space
{
//if space is found, check next character
i;
//check letter if lowercase
if(str[i]>='a' && str[i]<='z')
{
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}
}
}
printf("%s", str);
return 0;
}
uj5u.com熱心網友回復:
您的問題在于第二個回圈中過度使用該continue陳述句。for第二個 continue是毫無意義的,因為控制到達回圈的結尾,無論如何,在你擁有它的那一點之后。
但是第一個 continue實際上是導致錯誤的原因:洗掉元音后,修改后的字串中的第一個字符將是一個空格——因此,if將進入第二個回圈內的第一個塊,這將跳過對小寫字母的檢查跟隨空間。
洗掉這些continue陳述句將修復您的代碼。
另外,請注意,您可以使用islowerandtoupper函式來檢查小寫字母并轉換為大寫:
#include <stdio.h>
#include <string.h>
#include <ctype.h> // For islower and toupper
int main()
{
char str[] = "I am Iron Man";
size_t i, j, len = 0;
len = strlen(str);
// Accepting input.
for (i = 0; i < len; i )
{
// Check vowels.
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
// delete vowel syntax
for (j = i; j < len; j )
{
// Store after removing vowels
str[j] = str[j 1];
}
i--;
len--;
}
str[len 1] = '\0';
}
for (i = 0; str[i] != '\0'; i )
{
//check first character is lowercase alphabet
if (i == 0)
{
if (islower(str[i])) {
str[i] = toupper(str[i]);
}
// A "continue" here is wrong ... it will skip the following check for a lowercase letter
}
if (str[i] == ' ') //check space
{
//if space is found, check next character
i;
//check letter if lowercase
if (islower(str[i]))
{
str[i] = toupper(str[i]);
// No need for a "continue" here ... we're already at the end of the loop
}
}
}
printf("%s\n", str);
return 0;
}
uj5u.com熱心網友回復:
我有另一個解決方案給你,可能更容易理解:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char str[] = "I am Iron Man";
char *in;
char *out;
int up = 1; // very simple state, if "up" then next character should be made upper
for (in = str, out = str; *in; in ) {
if (strchr("aeiouAEIOU", *in) != NULL) {
// do nothing
} else if (*in == ' ') {
*out = *in;
up = 1; // we see a space, so next letter should be upper
} else if (up) {
*out = toupper(*in);
up = 0; // we see a letter (or other character), ignore case
} else {
*out = *in;
}
}
*out = '\0';
printf("%s\n", str);
}
或者,如果您不喜歡/不理解指標語法:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char str[] = "I am Iron Man";
int i;
int o;
int up = 1; // very simple state, if "up" then next character should be made upper
for (i = 0, o = 0; str[i]; i ) {
if (strchr("aeiouAEIOU", str[i]) != NULL) {
// do nothing
} else if (str[i] == ' ') {
str[o ] = str[i];
up = 1; // we see a space, so next letter should be upper
} else if (up) {
str[o ] = toupper(str[i]);
up = 0; // we see a letter (or other character), ignore case
} else {
str[o ] = str[i];
}
}
str[o] = '\0';
printf("%s\n", str);
}
在這兩種情況下,都使用了一個非常簡單的狀態。對于更復雜的條件,您應該了解狀態機。在這種情況下,up狀態指示下一個字母應大寫。
請注意,如果要洗掉前導空格,則在“洗掉”元音之后,需要稍微修改邏輯:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char str[] = "I am Iron Man";
char *in = str; // we initialize in and out here already
char *out = str;
int up = 1; // very simple state, if "up" then next chacter should be made upper
// we skip leading vowels AND spaces, this is a special case
while (*in && (strchr("aeiouAEIOU ", *in) != NULL)) {
in ;
}
// now we are at the first character that is not a vowel or space
for ( ; *in; in ) {
if (strchr("aeiouAEIOU", *in) != NULL) {
// do nothing
} else if (*in == ' ') {
*out = *in;
up = 1; // we see a space, so next letter should be upper
} else if (up) {
*out = toupper(*in);
up = 0; // we see a letter (or other character), ignore case
} else {
*out = *in;
}
}
*out = '\0';
printf("%s\n", str);
}
好吧,現在你有幾個例子需要研究,它們采用了一些不同的方法。看看你是否理解邏輯,并嘗試使其其他字符像 eg(和)分隔單詞。
uj5u.com熱心網友回復:
問題之一是你有太多的代碼。它遍歷整個陣列一次以去除元音,然后再次調整每個單詞的第一個字母的大小寫。想象一下,這是處理以 Gb 為單位的資料。第二遍是不必要的。
(而且,有一些標準庫函式isalpha(),toupper()你應該使用它們。不要用“幻數”撰寫代碼。)
值得研究程式的“流控制”,而不是訴諸任意的“繼續”陳述句來影響該流。
還值得從 中的最小代碼塊從頭開始main(),然后在一個(或多個)函式中開發您的演算法。避免將一個長的、線性的程式全部放在里面的趨勢main()。如果您可以將功能放入“隔間”,那么隨著程式變得越來越復雜,每個功能都可以被開發和測驗并被遺忘。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// A single pass "compacts" the data (no vowels) while also using some single operations
//tracking changing from one word to the next (first letter to uppercase.)
char *func( char *str ) {
for ( int d = 0, s = 0, up = 0; (str[d] = str[s]) != '\0'; s )
if( !strchr( " aeiouAEIOU" !!up, str[d] ) ) {
if( str[d] == ' ' )
up = 1;
else if( up < 2 )
up , str[d] = (char)toupper( (unsigned char)str[d] );
d ; // 'd'estination idx only increments here!
}
return str;
}
int main(void) {
// sample test strings
char *strs[] = {
"I am Iron Man",
" I am Iron Man ",
"Iron Man am I",
" Iron Man am I",
"The man of steel",
" The man of steel",
};
for( size_t i = 0; i < sizeof strs/sizeof strs[0]; i )
puts( func( strs[i] ) );
return 0;
}
M Rn Mn
M Rn Mn
Rn Mn M
Rn Mn M
Th Mn F Stl
Th Mn F Stl
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520232.html
標籤:数组C细绳
下一篇:在python中增加字串中的數字
