Por ejemplo, Abraham Lincoln en AB, RA, HA ML, IN, CO, LN. Sin usar string.ho stdlib.h
#include <stdio.h>
int main ()
{
printf("Enter the string: ");
char inputstring[50];
scanf("%[^\n]", inputstring);
char *p = inputstring;
char *k = inputstring;
while (*p != '\0')
{
printf ("%c%c ", *p, *k);
p++;
}
return 0;
}
Según el enunciado del problema, es necesario:
toUpper()
.', '
solo para el primer par que no sea
A continuación se muestra la muestra de salida del siguiente código
Enter the string: abraham lincoln
AB, RA, HA, ML, IN, CO, LN
Enter the string: abcde fghijk
AB, CD, EF, GH, IJ, K
Código:
#include <stdio.h>
char myToUpper(char c) {
if(c >= 97 && c <= 122) {
c = c-32;
} //end if
return c;
}
int main(void) {
printf("Enter the string: ");
char inputstring[50] = {0};
scanf("%[^\n]", inputstring);
char *p = inputstring;
int counter=0;
char firstChar, secondChar;
int isFirstPair = 1; //this is to handle the flag for first pair, used as the counter for comma character (only first pair do not need comma)
while (*p != 0) {
//this is to change between state (1st and 2nd character), need to exclude space
if (*p != ' ') { //only process non-space character, you can add more character filtering if needed
if (counter == 0) { //first char
firstChar = *p;
counter=1;
} else { //if we are on the 2nd character, then capitalize it and print it (along with the comma if needed)
secondChar = *p;
if (isFirstPair == 1) {
isFirstPair = 0;
} else {
printf(", ");
} //end else
printf("%c%c", myToUpper(firstChar), myToUpper(secondChar));
counter=0;
} //end else
} //end if
p++;
} //end while
//this is to handle when there are odd number of non-space characters
if (counter == 1) {
if (isFirstPair == 1) { //this is for the case where there is only 1 character input
printf("%c", myToUpper(firstChar));
} else {
printf(", %c", myToUpper(firstChar));
} //end else
} //end if
return 0;
}
Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.
En caso de infracción, por favor [email protected] Eliminar
Déjame decir algunas palabras