将带有某人姓名的字符串转换为姓名缩写?

时间:2016-03-01 23:19:38

标签: c

我只想让用户输入他们名字的字符串,我的程序遍历字符串并将其转换为首字母。然后输出首字母。例如,input = John Darly Smith;输出= J.D.S

这是我到目前为止所拥有的:

#include <stdio.h>

int main() {
    char name[50];
    char initials[5];
    int i = 0;
    int j = 1;

    printf("Enter your name: ");
    fgets(name, 10, stdin);

    initials[0] = name[0];

    while (name[i] != '\0') {
        if (name[i] == ' ') {
            initials[j] = name[i];
            initials[j + 1] = '.';
            j++;
        }
        i++;
    }
    printf("%s", initials);

    return 0;
}

编辑:代码无法按照我的意图运作。当我输入John Smith之类的内容时,会输出J .

4 个答案:

答案 0 :(得分:1)

以下是您的计划的工作版本:

#include <stdio.h>

int main()
{
    char name[50] = " ", initials[10] = " ";
    int counter_n = 1;
    int counter_i = 2;

    printf("Enter your name: ");
    fgets(name, sizeof(name), stdin);

    initials[0] = name[0];
    initials[1] = '.';

    while (name[counter_n] != '\0') {
        if (name[counter_n] == ' ') {
            initials[counter_i] = name[counter_n + 1];
            counter_n++;
            initials[counter_i + 1] = '.';
            counter_i += 2;     
        }
        counter_n++;            
    }
    initials[counter_i] = '\0';

    printf("Your initials are: %s\n", initials);

    return 0;
}
  • fgets(name,sizeof(name),stdin);最多读取(sizeof(name) - 1)chars,留下一个'\ 0'。在这种情况下,sizeof(name)= 50,因此fgets()最多可读取49个字符。
  • 添加'。'在第一个初始化之后,让计数器从名称[1]和初始[2]开始。
  • 通过数组正确迭代,您没有在代码中正确递增计数器。

答案 1 :(得分:0)

你的while循环中存在一个问题,你将name [i]设置为空格。

while(name[i] != '\0'){
    if(name[i] == ' '){
        // Selecting the next letter instead of a space
        initials[j] = name[i + 1]; // i + 1 instead of i
        initials[j + 1] = '.';
        j += 2; // You are also only jumping over the initial not the dot
    }
    i++;
}

您还需要在第一个首字母后添加一个点。

答案 2 :(得分:0)

代码中的一些修改:

#include<stdio.h>

int main(){

char name[50];
char initials[7]; //at least 7 for 3 chars, 3 dots, 1 terminating char.
int i = 0;
int j = 0;   //No need to do special handling for first char

printf("Enter your name: ");
fgets(name, 49, stdin);  // changed to 49

//Removed: No need to do special handling for first char

char c=name[i];  //Need to save initial somewhere.

while(name[i] != '\0'){

    if(name[i] == ' '){

        initials[j] = c;
        initials[j + 1] = '.';
        j+=2;
        c = name[i+1];
    }

    i++;

}

答案 3 :(得分:0)

要修复的示例

#include <stdio.h>
#include <ctype.h>

#define MAX_LEN 50 // 49 + 1

int main(void){
    char name[MAX_LEN];
    char initials[MAX_LEN/2];//5 is small
    char prev = ' ';
    int i, j = 0;

    printf("Enter your name: ");
    fgets(name, sizeof name, stdin);//To use sizeof(name) to the buffer size

    for(i = 0; name[i] != '\0'; ++i){
        if(name[i] != ' ' && prev == ' '){//edge of top of word
            initials[j++] = toupper(name[i]);
            initials[j++] = '.';
        }
        prev = name[i];
    }
    if(j)
        --j;//Back One for last '.'
    initials[j] = '\0';//set null character

    printf("\"%s\"\n", initials);

    return 0;
}