Tb penei um cado pra fazer a 5 mas pensei na seguinte logica vou dividi as tarefas em partes primeiro com 'for' correndo e analisando qual caractere se repete no que se repetir marquei eles com valor 'null' depois corri outro 'for' descartei os valores null substituindo pelos valores seguintes no array. Com certeza deve haver outras milhares de formas e ate melhores de fazer isso.
minha funcao ficou assim:
char *delrpts(char str[]){ int count; int ccount; char tmprpt;
Por favor, estou muito agarrado no exercício 4, não consigo finalizar ele. Se alguem tiver feito, me de uma ajuda, por favor.
18 de julho de 2017 às 06:53
Anônimo disse...
Tentei fazer a número 00, na função strmsc(transforma em maiúscula) não consigo retornar a string, caso eu tente isso o programa para de funcionar, então botei printf
/*00. Crie uma função que transforma todos os caracteres de uma string em maiúsculos.*/ #include
int strlen(char *str) { int i, cont = 0; for(i = 0; str[i] != '\0'; i++){ cont++; } return cont; }
char toup(char letra) { int num; if((int)letra >= 65 && (int)letra <= 90 || (int)letra == 32) { //MAISCULAS ESTÃO ENTRE 65 E 90 EM ASCII E ESPAÇO É 32 letra = (int)letra+32; //SOMA 32 PARA TRANSFORMAR EM MINÚSCULA }
num = (int)letra; num -= 32; //TRANSFORMAR EM MAIÚSCULA NOVAMENTE letra = (char)num;
Uma outra forma de colocar a string em maiúscula é subtrair 32. Pois, o tipo char também é um inteiro. Na tabela ASCII, as letras maiúsculas ficam entre 97 e 122, e as letras minúsculas ficam entre 65 e 90. A diferença entre 97 e 65 é 32:
//00. Crie uma função que transforma todos os caracteres de uma string em maiúsculos.
#include
int lowercaseToUppercase ( char *vetorChar );
int main(void) { char vetorChar[100]; printf("\n\t Entre com uma String \n\n"); fflush(stdin); fgets(vetorChar, 101, stdin); lowercaseToUppercase (vetorChar); printf("\n%s \n", vetorChar); }
int lowercaseToUppercase ( char *vetorChar ) { int counter; for ( counter = 0; vetorChar [counter] != '\0'; counter++ ) if ( (vetorChar [counter] >= 'a') && (vetorChar [counter] <= 'z') ) vetorChar [counter] = vetorChar [counter] - 32; }
6 de novembro de 2017 às 10:08
Let's Learn disse...
//01. Crie uma função que transforma todos os caracteres de uma string em minúsculos.
#include
int uppercaseToLowercase ( char *vetorChar );
int main(void) { char vetorChar[100]; printf("\n\t Entre com uma String \n\n"); fflush(stdin); fgets(vetorChar, 101, stdin); uppercaseToLowercase (vetorChar); printf("\n%s \n", vetorChar); }
int uppercaseToLowercase ( char *vetorChar ) { int counter; for ( counter = 0; vetorChar [counter] != '\0'; counter++ ) if ( (vetorChar [counter] >= 'A') && (vetorChar [counter] <= 'Z') ) vetorChar [counter] += 32; }
6 de novembro de 2017 às 10:21
Let's Learn disse...
//02. Crie uma função que recebe uma string e um caractere, //e retorne o número de vezes que esse caractere aparece na string.
#include
int timesOfTheCharacter ( char *vetorChar, char *character );
int main(void) { char vetorChar[100], character; printf("\n\t Entre com uma String \n\n\t"); fflush(stdin); fgets(vetorChar, 101, stdin); printf("\n\t%s \n", vetorChar); printf("\n\t Entre com o caracter a ser contado: "); fflush(stdin); scanf(" %c", &character); timesOfTheCharacter (vetorChar, character); }
int timesOfTheCharacter ( char *vetorChar, char *character ) { int counter, times = 0; for ( counter = 0; vetorChar [counter] != '\0' ; counter++ ) if ( vetorChar [counter] == character) times++; printf("\n A Quantidade de vezes que o caracter '%c' aparece e: %d \n", character, times); }
6 de novembro de 2017 às 10:51
Let's Learn disse...
//03. Crie uma função que recebe uma string e um caractere, // e apague todas as ocorrências desses caractere na string.
#include
int deleteCharacter ( char *vetorChar, char *character );
int main(void) { char vetorChar[100], character; printf("\n\t Entre com uma String \n\n\t"); fflush(stdin); fgets(vetorChar, 101, stdin); printf("\n\t Entre com o caracter a ser apagado: "); fflush(stdin); scanf(" %c", &character); deleteCharacter (vetorChar, character); printf("\n\t%s \n", vetorChar); }
// Os includes sao: //#include //#include //#define DIMENSION 500
// A funao pedida foi denominada por: showsRepeatedCharacters
#include #include #define DIMENSION 500
int showsRepeatedCharacters(const char *vetorCharInput);
int main(void) { char vetorChar[DIMENSION] = {NULL}; printf("\t Entre com uma String cujo o numero de caracteres nao seja maior que %d \n\n\t\t ", DIMENSION); fflush(stdin); fgets(vetorChar, DIMENSION + 1, stdin); printf("\n\t A string aceita com no maximo %d caracteres e:\n", DIMENSION); printf("\n\t String = %s \n", vetorChar); showsRepeatedCharacters(vetorChar); }
int showsRepeatedCharacters(const char *vetorCharInput) { int counterCopyInput; char vetorCharCopy[DIMENSION] = {NULL}; for ( counterCopyInput = 0; vetorCharInput[counterCopyInput] != '\0'; counterCopyInput++ ) { vetorCharCopy[counterCopyInput] = vetorCharInput[counterCopyInput]; }
// Os includes sao: //#include stdio.h //#include string.h //#define DIMENSION 500
// A função pedida foi denominada por: showsRepeatedCharactersAndDelete
#include #include #define DIMENSION 500
int showsRepeatedCharactersAndDelete (const char *vetorCharInput);
int main(void) { char vetorChar[DIMENSION] = {NULL}; printf("\t Entre com uma String cujo o numero de caracteres nao seja maior que %d \n\n\t\t ", DIMENSION); fflush(stdin); fgets(vetorChar, DIMENSION + 1, stdin); printf("\n\n\t A string aceita com no maximo %d caracteres e:\n", DIMENSION); printf("\n\t String = %s \n\n", vetorChar); showsRepeatedCharactersAndDelete(vetorChar); }
int showsRepeatedCharactersAndDelete(const char *vetorCharInput) { int counterCopyInput; char vetorCharCopy[DIMENSION] = {NULL}; for ( counterCopyInput = 0; vetorCharInput[counterCopyInput] != '\0'; counterCopyInput++ ) { vetorCharCopy[counterCopyInput] = vetorCharInput[counterCopyInput]; }
int counterDeleteRepeated; char characterDelete; for ( counterDeleteRepeated = 0 ; FinalRepeated [counterDeleteRepeated] != '\0' ; counterDeleteRepeated++ ) { characterDelete = FinalRepeated[counterDeleteRepeated]; int counterInput, deleteCounter; for ( counterInput = 0; vetorCharCopy [counterInput] != '\0' ; counterInput++ ) if ( vetorCharCopy [counterInput] == characterDelete) { deleteCounter = counterInput; for ( deleteCounter; vetorCharCopy [deleteCounter] != '\0' ; deleteCounter++ ) vetorCharCopy [deleteCounter] = vetorCharCopy [deleteCounter + 1]; counterInput--; } } printf("\n\t String sem os caracteres repetidos e = \n\n"); printf("\t\t %s", vetorCharCopy);
}
13 de novembro de 2017 às 08:13
Anônimo disse...
Alguem fez o Desafio 01: Strings que gaguejam. Estou com dificuldade nele...
22 de junho de 2018 às 10:11
Usando os conceitos ensinados aqui em nossa apostila de C, especificamente sobre Strings em C, crie funções que façam as seguintes tarefas:
Clique aqui e obtenha seu certificado de programação C! Entre já no Mercado de Trabalho!
Baixe o conteúdo do site: Apostila de C
Exercícios sobre Strings em C
postado por Programação Progressiva às 09:59 em 15 de mar. de 2013
12 Comentários
Fechar esta janela Ir para formulário de comentárioRealmente muito boas esse tutorial c. Estou aprendendo bastante.
Contudo, no que diz respeito ao exercício 5 sobre strings estou com dificuldade de retirar os caracteres repetidos da string.
Se puder dar uma luz sobre isso eu ficaria bastante grato.
28 de março de 2014 às 16:28
Tb penei um cado pra fazer a 5 mas pensei na seguinte logica vou dividi as tarefas em partes primeiro com 'for' correndo e analisando qual caractere se repete no que se repetir marquei eles com valor 'null' depois corri outro 'for' descartei os valores null substituindo pelos valores seguintes no array. Com certeza deve haver outras milhares de formas e ate melhores de fazer isso.
minha funcao ficou assim:
char *delrpts(char str[]){
int count;
int ccount;
char tmprpt;
for(count=0;str[count]!='\0';count++){
for(ccount=(count+1);str[ccount]!='\0';ccount++){
tmprpt = str[count];
if(tmprpt == str[ccount]){
str[count] = -1;
str[ccount] = -1;
}
}
}
for(count=0;str[count]!='\0';count++){
if(str[count] == -1){
for(ccount=count;str[ccount]!='\0';ccount++){
str[ccount] = str[(ccount+1)];
}
}
}
return str;
}
6 de novembro de 2014 às 08:17
Por favor, estou muito agarrado no exercício 4, não consigo finalizar ele. Se alguem tiver feito, me de uma ajuda, por favor.
18 de julho de 2017 às 06:53
Tentei fazer a número 00, na função strmsc(transforma em maiúscula) não consigo retornar a string, caso eu tente isso o programa para de funcionar, então botei printf
/*00. Crie uma função que transforma todos os caracteres de uma string em maiúsculos.*/
#include
int strlen(char *str) {
int i, cont = 0;
for(i = 0; str[i] != '\0'; i++){
cont++;
}
return cont;
}
char toup(char letra) {
int num;
if((int)letra >= 65 && (int)letra <= 90 || (int)letra == 32) { //MAISCULAS ESTÃO ENTRE 65 E 90 EM ASCII E ESPAÇO É 32
letra = (int)letra+32; //SOMA 32 PARA TRANSFORMAR EM MINÚSCULA
}
num = (int)letra;
num -= 32; //TRANSFORMAR EM MAIÚSCULA NOVAMENTE
letra = (char)num;
return letra; //RETORNA O CARACTER
}
void strmsc(char *str) {
int i;
for(i = 0; i < strlen(str); i++) {
str[i] = toup(str[i]);
printf("%c", str[i]);
}
}
int main(void) {
char palavra[21];
printf("Digite uma palavra: ");
scanf("%21[^\n]", palavra);
printf("%s", palavra);
printf("\nPALAVRA EM MAIUSCULA: "); strmsc(palavra);
}
12 de agosto de 2017 às 23:11
Uma outra forma de colocar a string em maiúscula é subtrair 32. Pois, o tipo char também é um inteiro. Na tabela ASCII, as letras maiúsculas ficam entre 97 e 122, e as letras minúsculas ficam entre 65 e 90. A diferença entre 97 e 65 é 32:
char *strlwr(char *s) {
for(int i=0; s[i] != '\0'; i++) {
if(s[i] >= 97 && s[i] <= 122) {
s[i] -= 32;
}
}
return s;
}
12 de outubro de 2017 às 12:03
//00. Crie uma função que transforma todos os caracteres de uma string em maiúsculos.
#include
int lowercaseToUppercase ( char *vetorChar );
int main(void)
{
char vetorChar[100];
printf("\n\t Entre com uma String \n\n");
fflush(stdin);
fgets(vetorChar, 101, stdin);
lowercaseToUppercase (vetorChar);
printf("\n%s \n", vetorChar);
}
int lowercaseToUppercase ( char *vetorChar )
{
int counter;
for ( counter = 0; vetorChar [counter] != '\0'; counter++ )
if ( (vetorChar [counter] >= 'a') && (vetorChar [counter] <= 'z') )
vetorChar [counter] = vetorChar [counter] - 32;
}
6 de novembro de 2017 às 10:08
//01. Crie uma função que transforma todos os caracteres de uma string em minúsculos.
#include
int uppercaseToLowercase ( char *vetorChar );
int main(void)
{
char vetorChar[100];
printf("\n\t Entre com uma String \n\n");
fflush(stdin);
fgets(vetorChar, 101, stdin);
uppercaseToLowercase (vetorChar);
printf("\n%s \n", vetorChar);
}
int uppercaseToLowercase ( char *vetorChar )
{
int counter;
for ( counter = 0; vetorChar [counter] != '\0'; counter++ )
if ( (vetorChar [counter] >= 'A') && (vetorChar [counter] <= 'Z') )
vetorChar [counter] += 32;
}
6 de novembro de 2017 às 10:21
//02. Crie uma função que recebe uma string e um caractere,
//e retorne o número de vezes que esse caractere aparece na string.
#include
int timesOfTheCharacter ( char *vetorChar, char *character );
int main(void)
{
char vetorChar[100], character;
printf("\n\t Entre com uma String \n\n\t");
fflush(stdin);
fgets(vetorChar, 101, stdin);
printf("\n\t%s \n", vetorChar);
printf("\n\t Entre com o caracter a ser contado: ");
fflush(stdin);
scanf(" %c", &character);
timesOfTheCharacter (vetorChar, character);
}
int timesOfTheCharacter ( char *vetorChar, char *character )
{
int counter, times = 0;
for ( counter = 0; vetorChar [counter] != '\0' ; counter++ )
if ( vetorChar [counter] == character)
times++;
printf("\n A Quantidade de vezes que o caracter '%c' aparece e: %d \n", character, times);
}
6 de novembro de 2017 às 10:51
//03. Crie uma função que recebe uma string e um caractere,
// e apague todas as ocorrências desses caractere na string.
#include
int deleteCharacter ( char *vetorChar, char *character );
int main(void)
{
char vetorChar[100], character;
printf("\n\t Entre com uma String \n\n\t");
fflush(stdin);
fgets(vetorChar, 101, stdin);
printf("\n\t Entre com o caracter a ser apagado: ");
fflush(stdin);
scanf(" %c", &character);
deleteCharacter (vetorChar, character);
printf("\n\t%s \n", vetorChar);
}
int deleteCharacter ( char *vetorChar, char *character )
{
int counter, deleteCounter;
for ( counter = 0; vetorChar [counter] != '\0' ; counter++ )
if ( vetorChar [counter] == character)
{
deleteCounter = counter;
for ( deleteCounter; vetorChar [deleteCounter] != '\0' ; deleteCounter++ )
vetorChar [deleteCounter] = vetorChar [deleteCounter + 1];
counter--;
}
}
6 de novembro de 2017 às 13:22
//04. Crie uma função que mostra os caracteres de uma string são repetidos.
//http://www.cprogressivo.net/2013/03/Exercicios-sobre-strings-em-C.html
// Os includes sao:
//#include
//#include
//#define DIMENSION 500
// A funao pedida foi denominada por: showsRepeatedCharacters
#include
#include
#define DIMENSION 500
int showsRepeatedCharacters(const char *vetorCharInput);
int main(void)
{
char vetorChar[DIMENSION] = {NULL};
printf("\t Entre com uma String cujo o numero de caracteres nao seja maior que %d \n\n\t\t ", DIMENSION);
fflush(stdin);
fgets(vetorChar, DIMENSION + 1, stdin);
printf("\n\t A string aceita com no maximo %d caracteres e:\n", DIMENSION);
printf("\n\t String = %s \n", vetorChar);
showsRepeatedCharacters(vetorChar);
}
int showsRepeatedCharacters(const char *vetorCharInput)
{
int counterCopyInput;
char vetorCharCopy[DIMENSION] = {NULL};
for ( counterCopyInput = 0; vetorCharInput[counterCopyInput] != '\0'; counterCopyInput++ )
{
vetorCharCopy[counterCopyInput] = vetorCharInput[counterCopyInput];
}
char RepeatedCharacters[DIMENSION * 1000] = {NULL};
int counterVetorCharCopy, counterRepeatedCharacters, REPEAT = 0;
for ( counterVetorCharCopy = 0; vetorCharCopy [counterVetorCharCopy] != '\0' ; counterVetorCharCopy++ )
{
for ( counterRepeatedCharacters = 0; vetorCharCopy [counterRepeatedCharacters] != '\0' ; counterRepeatedCharacters++ )
{
if (( counterVetorCharCopy != counterRepeatedCharacters ) && ( vetorCharCopy [counterVetorCharCopy] == vetorCharCopy [counterRepeatedCharacters] ))
{
RepeatedCharacters [REPEAT] = vetorCharCopy [counterVetorCharCopy];
REPEAT++;
}
}
}
char FinalRepeated[100] = {NULL};
int counterFinalRepeated;
char character;
for ( counterFinalRepeated = 0 ; strlen(RepeatedCharacters) >= 1 ; counterFinalRepeated++ )
{
character = RepeatedCharacters[0];
FinalRepeated [counterFinalRepeated] = RepeatedCharacters[0];
int counterInput, deleteCounter;
for ( counterInput = 0; RepeatedCharacters [counterInput] != '\0' ; counterInput++ )
if ( RepeatedCharacters [counterInput] == character)
{
deleteCounter = counterInput;
for ( deleteCounter; RepeatedCharacters [deleteCounter] != '\0' ; deleteCounter++ )
RepeatedCharacters [deleteCounter] = RepeatedCharacters [deleteCounter + 1];
counterInput--;
}
}
printf("\t Os caracteres repetidos sao = {%s} \n", FinalRepeated );
}
10 de novembro de 2017 às 15:28
//05. Crie uma função que retira todas os caracteres repetidos de uma string.
//http://www.cprogressivo.net/2013/03/Exercicios-sobre-strings-em-C.html
// Os includes sao:
//#include stdio.h
//#include string.h
//#define DIMENSION 500
// A função pedida foi denominada por: showsRepeatedCharactersAndDelete
#include
#include
#define DIMENSION 500
int showsRepeatedCharactersAndDelete (const char *vetorCharInput);
int main(void)
{
char vetorChar[DIMENSION] = {NULL};
printf("\t Entre com uma String cujo o numero de caracteres nao seja maior que %d \n\n\t\t ", DIMENSION);
fflush(stdin);
fgets(vetorChar, DIMENSION + 1, stdin);
printf("\n\n\t A string aceita com no maximo %d caracteres e:\n", DIMENSION);
printf("\n\t String = %s \n\n", vetorChar);
showsRepeatedCharactersAndDelete(vetorChar);
}
int showsRepeatedCharactersAndDelete(const char *vetorCharInput)
{
int counterCopyInput;
char vetorCharCopy[DIMENSION] = {NULL};
for ( counterCopyInput = 0; vetorCharInput[counterCopyInput] != '\0'; counterCopyInput++ )
{
vetorCharCopy[counterCopyInput] = vetorCharInput[counterCopyInput];
}
char RepeatedCharacters[DIMENSION * 1000] = {NULL};
int counterVetorCharCopy, counterRepeatedCharacters, REPEAT = 0;
for ( counterVetorCharCopy = 0; vetorCharCopy [counterVetorCharCopy] != '\0' ; counterVetorCharCopy++ )
{
for ( counterRepeatedCharacters = 0; vetorCharCopy [counterRepeatedCharacters] != '\0' ; counterRepeatedCharacters++ )
{
if (( counterVetorCharCopy != counterRepeatedCharacters ) && ( vetorCharCopy [counterVetorCharCopy] == vetorCharCopy [counterRepeatedCharacters] ))
{
RepeatedCharacters [REPEAT] = vetorCharCopy [counterVetorCharCopy];
REPEAT++;
}
}
}
char FinalRepeated[100] = {NULL};
int counterFinalRepeated;
char character;
for ( counterFinalRepeated = 0 ; strlen(RepeatedCharacters) >= 1 ; counterFinalRepeated++ )
{
character = RepeatedCharacters[0];
FinalRepeated [counterFinalRepeated] = RepeatedCharacters[0];
int counterInput, deleteCounter;
for ( counterInput = 0; RepeatedCharacters [counterInput] != '\0' ; counterInput++ )
if ( RepeatedCharacters [counterInput] == character)
{
deleteCounter = counterInput;
for ( deleteCounter; RepeatedCharacters [deleteCounter] != '\0' ; deleteCounter++ )
RepeatedCharacters [deleteCounter] = RepeatedCharacters [deleteCounter + 1];
counterInput--;
}
}
printf("\t Os caracteres repetidos sao = {%s} \n", FinalRepeated );
int counterDeleteRepeated;
char characterDelete;
for ( counterDeleteRepeated = 0 ; FinalRepeated [counterDeleteRepeated] != '\0' ; counterDeleteRepeated++ )
{
characterDelete = FinalRepeated[counterDeleteRepeated];
int counterInput, deleteCounter;
for ( counterInput = 0; vetorCharCopy [counterInput] != '\0' ; counterInput++ )
if ( vetorCharCopy [counterInput] == characterDelete)
{
deleteCounter = counterInput;
for ( deleteCounter; vetorCharCopy [deleteCounter] != '\0' ; deleteCounter++ )
vetorCharCopy [deleteCounter] = vetorCharCopy [deleteCounter + 1];
counterInput--;
}
}
printf("\n\t String sem os caracteres repetidos e = \n\n");
printf("\t\t %s", vetorCharCopy);
}
13 de novembro de 2017 às 08:13
Alguem fez o Desafio 01: Strings que gaguejam.
Estou com dificuldade nele...
22 de junho de 2018 às 10:11