幫助!我用C編寫一個程序來獲取所有的英文縮寫 - 我一無所知指針所以讓我們嘗試從那些遠離 - 這是我到目前爲止有:從字符串(名稱)中獲取首字母的程序,無法刪除所有空格
#include <stdio.h>
//CS50 Library for 'get_string() user input'
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main(void){
printf("Enter your name: ");
//User input for string
string s = get_string();
int i = 0;
//Determine whether the first chars are space - if not print char
if(s[0] != isspace(s[0])){
printf("%c",s[0]);
}
//loop through each Char - determine char is not space
while(s[i] != '\0'){
if(s[i] == ' '){
//if i'th char is space - add one to it thus printing char that comes after space
printf("%c", toupper(s[i+1]));
}
//advance i'th char count
i++;
}
printf("\n");
}
當我輸入「約翰·傑拉爾德·史密斯」該程序會返回「JGB」,但如果我嘗試輸入諸如「John gerald smith」(多個空格)之類的字詞,它似乎不會刪除任何空格。我仍然得到輸出的首字母,但我需要確保它不會打印任何空格。請幫忙!這是家庭作業,所以我不希望只是交出答案,但如果任何人都可以給我一些關於如何做的信息,我會非常感激。謝謝!
不要只是看當前字符,而且NEXT字符是否不是空格。小心邊界檢查(不要超過字符串的末尾!) – meisen99