Tema 7: Cadenas de caracteres



1. Crear un programa que tome una palabra y la escriba al revés (máximo de 10 letras):


                        #define _CRT_SECURE_NO_WARNINGS
                        #include < stdio.h>
                        #include < string.h>

                        //To define ctes....
                        #define MAX_CHAR 11

                        void main()
                        {
                            //To declare an array for saving the word...
                            char word[MAX_CHAR];
                            //To declare other variables...
                            char aux;
                            int i, j, counter = 0;

                            //Start the Program....
                            printf("\n\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$ BACKWARDS WORDS $$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");

                            //To introduce by keyboard...
                            printf("Please, introduce a word of maximum 10 letters: ");
                            fflush(stdin);
                            fgets(word,MAX_CHAR,stdin);
                            //To change the \n by \0...
                            for(i=0; i < MAX_CHAR; i++)
                            {
                                if(word[i] == '\n')
                                {
                                    word[i] = '\0';
                                }
                            }

                            //To do the main part of the program and to print it....
                            printf("\n\nThe backward word is: ");
                            for(i=strlen(word)-1; i>=0; i--)
                            {
                                printf("%c", word[i]);
                            }
                            printf(".\n\n");
                        }
                    

2. Crear un programa que verifique si una palabra es un palíndromo (máximo de 10 letras).
Un palíndromo es una palabra que se lee igual al revés que hacia adelante, como anna:


                        #define _CRT_SECURE_NO_WARNINGS
                        #include < stdio.h>
                        #include < string.h>

                        //To define ctes....
                        #define MAX_CHAR 11

                        void main()
                        {
                            //To declare an array for saving the introduced word...
                            char word[MAX_CHAR];
                            //To declare an array for saving the reversed word...
                            char reverse[MAX_CHAR];
                            //To declare other variables...
                            int i, j = 0, result;

                            //Start the Program....
                            printf("\n\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$ Palindrome WORDS $$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");

                            //To introduce by keyboard...
                            printf("Please, introduce a word of maximum 10 letters: ");
                            fflush(stdin);
                            fgets(word,MAX_CHAR,stdin);
                            //To change the \n by \0...
                            for(i=0; i < MAX_CHAR; i++)
                            {
                                if(word[i] == '\n')
                                {
                                    word[i] = '\0';
                                }
                            }

                            //To do the main part of the program and to print...
                            for(i=strlen(word)-1; i>=0; i--)
                            {
                                reverse[j] = word[i];
                                j++;
                            }

                            result = strcmp(word, reverse);
                            if(result == 0)
                            {
                                printf("\n\nThe word %s is a PALINDROME.\n\n", word);
                            }
                            else
                            {
                                printf("\n\nThe word %s is NOT a PALINDROME.\n\n", word);
                            }
                        }
                    

3. Crear un programa que tome una cadena y escriba los números en la cadena (máximo de 15 caracteres):


                        #define _CRT_SECURE_NO_WARNINGS
                        #include < stdio.h>
                        #include < string.h>

                        //To define ctes....
                        #define MAX_CHAR 16

                        void main()
                        {
                            //To declare an array for saving the string...
                            char tweet[MAX_CHAR];
                            //To declare an array for saving the numbers...
                            char numbers[MAX_CHAR];
                            //To declare other variables...
                            int i, j = 0;

                            //Start the Program....
                            printf("\n\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$ Numbers in a String $$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");

                            //To introduce by keyboard...
                            printf("Please, introduce a Tweet of maximum 15 characters: ");
                            fflush(stdin);
                            fgets(tweet,MAX_CHAR,stdin);
                            //To change the \n by \0...
                            for(i=0; i < MAX_CHAR; i++)
                            {
                                if(tweet[i] == '\n')
                                {
                                    tweet[i] = '\0';
                                }
                            }

                            //To find the numbers in the string...
                            for(i=0; i < strlen(tweet); i++)
                            {
                                if(tweet[i] >= '0' && tweet[i] <= '9')
                                {
                                    numbers[j] = tweet[i];
                                    j++;
                                }
                            }

                            //To print out all numbers of the string...
                            if(numbers[0] == '\0')
                            {
                                printf("\n\nThere are no numbers in the string %s.\n\n", tweet);
                            }
                            else if(numbers[1] == '\0')
                            {
                                printf("\n\nThe number, which is in the string %s, is: %s.\n\n", tweet, numbers);
                            }
                            else
                            {
                                printf("\n\nThe numbers, which are in the string %s, are: %s.\n\n", tweet, numbers);
                            }
                        }
                    

4. Crear un programa que tome dos palabras del usuario y verifique si la segunda palabra es parte de la primera (máximo de 15 caracteres):


                        #define _CRT_SECURE_NO_WARNINGS
                        #include < stdio.h>
                        #include < string.h>

                        //To define ctes....
                        #define MAX_CHAR 16

                        void main()
                        {
                            //To declare an array for saving the long word and the short word...
                            char string1[MAX_CHAR];
                            char string2[MAX_CHAR];
                            //To declare other variables...
                            int i;

                            //Start the Program....
                            printf("\n\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$ Part of the first word $$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");

                            //To introduce by keyboard...
                            printf("Please, introduce a string of maximum 15 characters: ");
                            fflush(stdin);
                            fgets(string1,MAX_CHAR,stdin);
                            printf("Please, introduce a shorter string than the previous one: ");
                            fflush(stdin);
                            fgets(string2,MAX_CHAR,stdin);
                            //To change the \n by \0...
                            for(i=0; i < MAX_CHAR; i++)
                            {
                                if(string1[i] == '\n')
                                {
                                    string1[i] = '\0';
                                }
                                if(string2[i] == '\n')
                                {
                                    string2[i] = '\0';
                                }
                            }

                            //To do the main part of the program and to print...
                            if(strstr(string1, string2))
                            {
                                printf("\n\nThe second word \"%s\" was found in the first word \"%s\".\n\n", string2, string1);
                            }
                            else
                            {
                                printf("\n\nThe second word \"%s\" was NOT found in the first word \"%s\".\n\n", string2, string1);
                            }
                        }
                    

5. Crear un programa que tome dos palabras del usuario y las concatene en una cadena con un espacio entre ellas y luego muestre la cadena final:


                        #define _CRT_SECURE_NO_WARNINGS
                        #include < stdio.h>
                        #include < string.h>

                        //To define ctes....
                        #define MAX_CHAR 16
                        #define MAX_FINAL 32

                        void main()
                        {
                            //To declare an array for saving the string.
                            char wordA[MAX_CHAR];
                            char wordB[MAX_CHAR];
                            char stringFinal[MAX_FINAL];

                            //To declare other variables...
                            int i, j;

                            //Start the Program....
                            printf("\n\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$ Concatenate two words in a string between one space $$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");

                            //To introduce by keyboard...
                            printf("Please, write the first word of maximum 15 characters: ");
                            fflush(stdin);
                            fgets(wordA,MAX_CHAR,stdin);
                            printf("Please, write the second word of maximum 15 characters: ");
                            fflush(stdin);
                            fgets(wordB,MAX_CHAR,stdin);
                            //To change the \n by \0...
                            for(i=0; i < MAX_CHAR; i++)
                            {
                                if(wordA[i] == '\n')
                                {
                                    wordA[i] = '\0';
                                }
                                if(wordB[i] == '\n')
                                {
                                    wordB[i] = '\0';
                                }
                            }

                            //To do the main part of the program...
                            for(i=0; i<=strlen(wordA); i++)
                            {
                                if(i == strlen(wordA))
                                {
                                    stringFinal[i] = ' ';
                                }
                                else
                                {
                                    stringFinal[i] = wordA[i];
                                }
                            }

                            j = strlen(wordA) + 1;
                            for(i=0; i<=strlen(wordB); i++)
                            {
                                stringFinal[j] = wordB[i];
                                j++;
                            }

                            //To print the result...
                            printf("\n\nThe concatenated string is: %s.\n\n", stringFinal);
                        }
                    

6. Crear un programa que tome una palabra y una letra y luego cuente cuántas veces aparece la letra en la palabra.
Si no se encuentra la letra en la palabra, muestra el mensaje de que no se encontró:


                        #define _CRT_SECURE_NO_WARNINGS
                        #include < stdio.h>
                        #include < string.h>

                        //To define ctes....
                        #define MAX_CHAR 16

                        void main()
                        {
                            //To declare an array for saving the word.
                            char word[MAX_CHAR];
                            char letter;
                            //To declare other variables...
                            int i, times = 0;

                            //Start the Program....
                            printf("\n\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$ Finding a letter in a word $$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");

                            //To introduce by keyboard...
                            printf("Please, write a word of maximum 15 characters: ");
                            fflush(stdin);
                            fgets(word,MAX_CHAR,stdin);
                            //To change the \n by \0...
                            for(i=0; i < MAX_CHAR; i++)
                            {
                                if(word[i] == '\n')
                                {
                                    word[i] = '\0';
                                }
                            }

                            fflush(stdin);
                            do
                            {
                                printf("Please, write a letter: ");
                                scanf(" %c", &letter);

                            }while((letter < 'A') || (letter > 'Z' && letter < 'a') || (letter > 'z'));

                            //To do the main part of the program...
                            for(i=0; i < strlen(word); i++)
                            {
                                if(word[i] == letter)
                                {
                                    times += 1;
                                }
                            }

                            //To print the number of times that the letter appear in the word...
                            if(times == 0)
                            {
                                printf("\n\nThe letter has NOT found in the word.\n\n");
                            }
                            else if(times == 1)
                            {
                                printf("\n\nThe letter appears %d time in the word.\n\n", times);
                            }
                            else
                            {
                                printf("\n\nThe letter appears %d times in the word.\n\n", times);
                            }
                        }
                    

7. Crear un programa que cambie los dígitos de una cadena por su representación en palabras (solo dígitos del 0 al 9).
Por ejemplo: 5 euros se debe cambiar por cinco euros:


                        #define _CRT_SECURE_NO_WARNINGS
                        #include < stdio.h>
                        #include < string.h>

                        //To define ctes....
                        #define MAX_CHAR 21

                        void main()
                        {
                            //To declare an array for saving the string.
                            char string[MAX_CHAR];
                            char string2[MAX_CHAR];
                            //To declare other variables...
                            int i, j;
                            char aux;

                            //Start the Program....
                            printf("\n\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$ Number - Word Representation $$$$$$$$$");
                            printf("\n");
                            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                            printf("\n");

                            //To introduce by keyboard...
                            printf("Please, write a price between 0 and 9, and the currency in words: ");
                            fflush(stdin);
                            fgets(string,MAX_CHAR,stdin);
                            //To change the \n by \0...
                            for(i=0; i < MAX_CHAR; i++)
                            {
                                if(string[i] == '\n')
                                {
                                    string[i] = '\0';
                                }
                            }

                            //To do the main part of the program...
                            for(i=0; i < strlen(string); i++)
                            {
                                if(string[i] >= '0' && string[i] <= '9')
                                {
                                    aux = string[i];
                                }
                            }

                            //To put the price in words...
                            switch(aux)
                            {
                                case '0':
                                    strcpy(string2, "zero");
                                    break;
                                case '1':
                                    strcpy(string2, "one");
                                    break;
                                case '2':
                                    strcpy(string2, "two");
                                    break;
                                case '3':
                                    strcpy(string2, "three");
                                    break;
                                case '4':
                                    strcpy(string2, "four");
                                    break;
                                case '5':
                                    strcpy(string2, "five");
                                    break;
                                case '6':
                                    strcpy(string2, "six");
                                    break;
                                case '7':
                                    strcpy(string2, "seven");
                                    break;
                                case '8':
                                    strcpy(string2, "eight");
                                    break;
                                case '9':
                                    strcpy(string2, "nine");
                                    break;
                            }

                            j = strlen(string2);
                            string2[j] = ' ';
                            j++;
                            for(i=0; i<=strlen(string); i++)
                            {
                                if((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
                                {
                                    string2[j] = string[i];
                                    j++;
                                }
                                else if(string[i] == '\0')
                                {
                                    string2[j] = '\0';
                                }
                            }

                            //To print the price and the currency in words...
                            printf("\n\nYour string has changed to: %s.\n\n", string2);
                        }
                    


Ubicación: Universidad de Novi Sad - Dr Zorana Đinđića 1, Novi Sad 21000, Vojvodina, Serbia

Teléfono: +381 21 485 2056

Correo electrónico: iro.ftn@uns.ac.rs



Copyright © 2023-2030 My Professional WEB - Todos los derechos reservados


Original text
Rate this translation
Your feedback will be used to help improve Google Translate