current position:Home>C language freshman PTA input an English sentence, change the first letter of each word into capital letters.

C language freshman PTA input an English sentence, change the first letter of each word into capital letters.

2022-02-02 18:54:06 CSDN Q & A

What's wrong ???

 

#include"stdio.h"
#include"string.h"
int main()
{
    int i,j;
    char a[101];
    gets(a);
     if(a[0]>='a'&&a[i]<='z') {
            a[0]-=32;

        }

    for(i=1;i<strlen(a);i++) {
        if(a[i]=='\0'){
             if(a[i+1]>='a'&&a[i+1]<='z') {
            a[i+1]-=32;

        }

        }

    }
    puts(a);
    return 0;
}
 

 

 




Refer to the answer 1:
#include"stdio.h"#include"string.h"int main(){    int j=0;    char a[101];    gets(a);     if(a[0]>='a'&&a[j]<='z')          {            a[0]-=32;        }    for(j=1;j<strlen(a);j++)     {        if(a[j]=='\0'||a[j]==32)        {             if(a[j+1]>='a'&&a[j+1]<='z')              {            a[j+1]-=32;            }        }    }    puts(a);    return 0;}

Your variables (i,j) No assignment first , Judge the space between words




Refer to the answer 2:



Refer to the answer 3:

For reference :

#include<stdio.h>#include<ctype.h>int main(){    char a[100+1], c;    int i;    gets(a);    if (a[0] != ' ') {        if (islower(a[0]))// Judge s[0] It's the beginning of the word             a[0] = toupper(a[0]);    }    for (i = 0; (c = a[i]) != '\0'; i++)    {        if (c == ' ' && a[i + 1] != ' ') {
   
   
   
   // Judge s[i+1] Is the beginning of a word             if (islower(a[i + 1]))// Determine whether it is a lowercase character                 a[i + 1] = toupper(a[i + 1]);        }    }    puts(a);    return 0;}



Refer to the answer 4:



Refer to the answer 5:
#include"stdio.h"#include"string.h"int main(){    int i=0, j=0;    char a[101];    gets_s(a);    if (a[0] >= 'a' && a[0] <= 'z') {        a[0] -= 32;    }    for (i = 1; i < strlen(a); i++) {        if (a[i] == ' ') {            if (a[i + 1] >= 'a' && a[i + 1] <= 'z') {                a[i + 1] -= 32;            }        }    }    puts(a);    return 0;}



Refer to the answer 6:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021854040937.html

Random recommended