7.1
EXERCÍCIOS PROPOSTOS – SOLUÇÃO
char Nome[25+1]; ou
char Nome[26];
7.2
7.2.1
7.2.2
int strcounta(char *s) /* isalpha: Necessário <ctype.h> */
{ int i, conta; for (i=conta=0 ; s[i]!='\0' ; i++) if (isalpha(s[i])) /* Se o caráter for alfabético */ conta++; /* Incrementar o contador */ return conta; }
char *init_str(char *s) { s[0] = '\0'; return s; }
7.2.3
int ult_ind_chr(char *s, char c) /* v1 – começar pelo princípio */ { int ind=-1; for (int i=0 ; s[i]!='\0' ; i++) if (s[i]==c) ind = i; return ind; }
int ult_ind_chr(char *s, char c) /* v2 – começar pelo fim */
{ for (int i=strlen(s)-1 ; i>=0 ; i--) if (s[i]==c) return i; return -1; }
7.2.4
7.2.5
/* tolower: Necessário <ctype.h> */
char *strlwr(char *str)
{ for (int i=0 ; str[i]!='\0' ; i++) str[i] = tolower(str[i]); return str; }
char *strnset(char *s, char ch, int n) /* Versão 1 */
{ for (int i=0 ; s[i]!='\0' && i<n ; i++) s[i]=ch; return s; }
char *strnset(char *s, char ch, int n) /* Versão 2 */
{ for (int i=0 ; s[i]!='\0' ; i++) if (i<n) s[i]=ch; else break; return s; }
7.2.6
7.2.7
/* toupper: Necessário <ctype.h> */ int stricmp(char *s1, char *s2)
{ int i=0; while (toupper(s1[i])==toupper(s2[i]) && s1[i]!='\0') i++; return (toupper(s1[i]) - toupper(s2[i])); }
char *repeticoes(char *s)
{ int i, j; for (i=j=0; s[i]!='\0'; i++) if (strCountChar(s, s[i])>1) /* Ocorre mais do que 1 vez */ s[j++] = s[i]; s[j]='\0'; return s; }
7.2.8
7.2.9
char n_esimo(char *s, int n)
{ return s[n-1]; /* Pois os índices dos arrays começam em 0 */ }
char *strpack(char *s)
{ int i, j; for (i=j=0; s[i]!='\0'; i++) if (s[i]!=s[j]) s[++j]=s[i];
s[++j]='\0'; return s; }
7.2.10
char *Entremeado(char *s, int n) { int i, j, len=strlen(s); if (n==0) return s; for (i=j=0; i<len ; i+=n+1) s[j++] = s[i]; s[j]='\0'; return s; }
7.2.11
7.2.12
#define SPACE ' '
char *xspace(char *s) { int i=strlen(s); /* i = nº carateres em s */ int n_espacos = strcountc(s, SPACE); /* Nº de Espaços em s */ int dim = 2*i-n_espacos; /* Dimensão total da string resultado */
s[dim--] ='\0'; /* Terminador */ for (--i ; i>=0 ; i--) if (s[i] == SPACE) s[dim--] = s[i]; /* É um espaço. Copiar apenas */ else { s[dim--]=SPACE; /* Colocar o espaço */ s[dim--]=s[i]; /* a seguir a este caráter */ } return s; }
char Max_Ascii(char *str)
{ char res='\0'; /* Menor código ASCII */ for (int i=0; str[i]!='\0';i++) res = res > str[i] ? res : str[i]; return res; }
7.2.13
char *prox_char(char *s) { for (int i=0; s[i]!='\0'; i++) s[i]=s[i]+1; /* Caráter ASCII Seguinte */ return s; }
7.2.14 Se o último caráter da tabela ASCII (255) existir na string , quando for incrementado em 1 unidade vai passar a 256, valor que não cabe em 1 byte. Como tal, o bit extra associado ao segundo byte é descartado e o valor armazenado vai corresponder ao caráter número ZERO. Funciona como se os códigos dos carateres dessem a volta e começassem de novo a partir do zero, sempre que cheguem ao maior valor possível de armazenar em 1 byte (255). Deste modo, a string pode vir a terminar não no
7.2.15
7.2.16
char '\0' de origem, mas numa posição anterior que correspondia à posição ocupada pelo último caráter da tabela ASCII na string original.
char *UpDown(char *s) /* Versão 1 */
{ for (int i=0; s[i]!='\0'; i++) if (i%2==0) /* Se for par */ s[i] = toupper(s[i]); else /* Se for impar */ s[i] = tolower(s[i]); return s; }
char *UpDown(char *s) /* Versão 2 */
{ for (int i=0;s[i]!='\0'; i++) s[i] = (i%2==0) ? toupper(s[i]) : tolower(s[i]); return s; }
De notar que ambas as soluções necessitam do #include <ctype.h> para que se possam usar as funções tolower e toupper
char *allspaces(char *s) /* Versão 1 */ { for (int i=0; s[i]!='\0'; i++) s[i]=' '; return s; }
ou
char *allspaces(char *s) /* Versão 2 */ { return strset(s, ' '); }
7.2.17
7.2.18
char *strijset(char *s, int i, int j, char ch) { if (i>=strlen(s)) return s; while (i<=j && s[i]!='\0') s[i++]=ch; return s; }
char *strduplica(char *s) { int i, len=strlen(s); for (i=0 ; i<len ; i++) s[i+len] = s[i]; s[i+len]='\0'; return s; }
7.2.19
/* Necessário #include <ctype.h> */
int atoi(char *s)
{ int i=0, res=0, sinal=1; if (s[0]=='-' || s[0]=='+') i=1; /* Começa a seguir ao 1º caráter */ if (s[0]=='-') sinal=-1; /* É um nº negativo */ for ( ; isdigit(s[i]) ; i++) res = res * 10 + s[i] - '0';
return res*sinal; }
Se a string for igual a 123 , o processamento é o seguinte:
res = 0
res = res*10 + 1 = 0*10 + 1 = 1
res = res*10 + 2 = 1*10 + 2 = 12
res = res*10 + 3 = 12*10 + 3 = 123
No entanto, é de salientar que, por exemplo, para obter o inteiro 4 correspondente ao caráter '4', é necessário subtrair o valor do código ASCII do caráter '0' .
Exemplo:
4 = '4' -'0'
7.2.20
char *wordupr(char *s) { s[0] = toupper(s[0]); /* O primeiro fica sempre em maiúsculas */ for (int i=1 ; s[i-1]!='\0' ; i++) if (s[i-1]==' ') /* Se o caráter anterior for um espaço */ s[i] = toupper(s[i]); else s[i] = tolower(s[i]);
return s; }
7.2.21
char *lower_upper(char *s) { int existem_trocas = 1, len = strlen(s); char tmp; /* Aux para a realização da troca de carateres */ while (existem_trocas) { existem_trocas = 0; for(int i=0 ; i<len-1 ; i++) if (isupper(s[i]) && islower(s[i+1])) { tmp=s[i]; s[i]=s[i+1]; s[i+1]=tmp; existem_trocas=1; } }
return s; }
7.2.22
char *All_Big(char *s) { int i, j; for (i=j=0; s[i]!='\0'; i++) if (isupper(s[i])) s[j++]=s[i]; s[j]= '\0'; return s; }
7.2.23
7.2.24
int is_len_OK(char *string, int comprimento) { return strlen(string)==comprimento; }
int is_Alfa_Digit(char *s)
{ for (int i=0;s[i]!='\0';i++) if (i%2==0) if (!isalpha(s[i])) return 0; else if (!isdigit(s[i])) return 0; return 1; }
7.2.25
7.2.26
char *Transform(char *s) /* Versão 1 */ { int len=strlen(s); for (int i=0; i<len; i++) if (i<len/2) s[i] = tolower(s[i]); else s[i] = toupper(s[i]); return s; }
char *Transform(char *s) /* Versão 2 */ { int len=strlen(s)-1; for (int i=0; i<=len; i++, len--) { s[i] = tolower(s[i]); s[len] = toupper(s[len]); } return s; }
char Ultimo(char *string) { if (*string=='\0') return '\0'; else return string[strlen(string)-1]; }
7.3
7.2.27
7.2.28
int isFile(char *string) { char *pos_ponto = strchr(string, '.'); if (pos_ponto==NULL) /* Não existe o '.' */ return 0; if (pos_ponto==string) /* Não existem chars à esquerda */ return 0; if (*(pos_ponto+1)=='\0') /* Não existem chars à direita */ return 0; return 1; }
char *str_asterisco(char*s, int n) { int i, len; for (i=0, len=strlen(s) ; i<len ; i+=n+1) s[i]='*'; return s; }
1: #include <stdio.h>
2: #include <ctype.h>
3: #include <string.h>
4:
5: #define OP_SAIR "SAIR"
6:
7: int strCountChar(char *s, char ch)
8: { 9: int i, conta;
10: for (i=conta=0 ; s[i]!='\0' ; i++)
11: if (s[i]==ch) /* Se for o caráter que procuramos */ 12: conta++; /* Incrementar o contador */ 13: return conta; 14: }
15:
16: char *strrev(char *s)
17: { 18: int i, len; 19: char aux; 20:
21: for (i=0, len=strlen(s)-1 ; i < len ; i++, len--)
22: { 23: aux=s[i];
24: s[i] = s[len]; 25: s[len]= aux; 26: }
27: return s; 28: }
29:
30: char *wordupr(char *s)
31: { 32: s[0] = toupper(s[0]); /* O primeiro fica sempre em maiúsculas */
33:
34: for (int i=1 ; s[i-1]!='\0' ; i++)
35: if (s[i-1]==' ') /* Se o caráter anterior for um espaço */
36: s[i] = toupper(s[i]);
37: else
38: s[i] = tolower(s[i]);
39:
40: return s;
41: }
42:
43: int stricmp(char *s1, char *s2)
44: {
45: int i=0;
46: while (toupper(s1[i])==toupper(s2[i]) && s1[i]!='\0')
47: i++;
48: return (toupper(s1[i]) - toupper(s2[i]));
49: }
50:
51:
52: /*
53: * Coloca no Parâmetro Apelido a última palavra da string Nome. 54: * Em seguida retira essa palavra da string Nome colocando um '\0'
55: */
56:
57: void separa(char *nome, char *apelido)
58: {
59: int i, j;
60: if (strCountChar(nome, ' ')==0) /* Existe apenas uma ou zero
61: palavras no nome */
62: {
63: apelido[0]='\0';
64: return;
65: }
66:
67: /* Existe a garantia de que existe um apelido */
68:
69: for (i=strlen(nome)-1, j=0 ; nome[i]!=' ' ; )
70: apelido[j++] = nome[i--];
71:
72: apelido[j]='\0'; /* Terminar a string Apelido */
73: nome[i]='\0'; /* Retirar o Apelido da String Nome */
74:
75: // Como a string Apelido foi colocada do fim para o princípio é 76: // necessário invertê-la
77:
78: strrev(apelido);
79: }
80:
81: int main(void)
82: {
83: char nome[100], apelido[50];
84:
85: for (;;) /* Equivalente a while (1) */
86: {
87: printf("Nome: ");
88: gets(nome);
89: if (stricmp(nome, OP_SAIR)==0)
90: break; /* Sair do Programa */ 91: separa(nome, apelido);
92: printf("%s, %s\n", wordupr(apelido), wordupr(nome)); 93: }
94: return 0; 95: }