View Full Version : strings em C ou em Basic
Angelizer 26-10-2006, 10:37 bom dia a tds queria saber se em C existem funçoes,no stdlib.h para:
--> devolver o numero de caracters de uma string..
--> determinar a posiçao de uma substring dentro de uma string
--> extrair uma string, apartir de uma posiçao, X caracters
ou seja, em pseudocodigo é, por ordem: lenght(str), index(str,substr) e sub(str,i,x) mas em C nao faço ideia com é...
ou entao a mesma coisa mas para Basic
ja descubri 2
->strlen(string) = length
->strstr(string,substring) = index
so me falta mesmo o SUB =/
se alguem me puder ajudar agradecia thx
MaxDamage 26-10-2006, 10:54 Em C usa a library <string.h> para teres acesso ás funções de manipulação de strings. Vê aqui (http://www-ccs.ucsd.edu/c/string.html) mais info ;)
sapropel 26-10-2006, 15:37 algo como:
//buffer tem de estar inicializado com com o char '\0' já inserido no final do array.
char* substr(const char* src, char* buffer, int index, int numchars)
{
int i = 0, n= 0;
//buffer tem espaço suficiente?
if(numchars > strlen(buffer))
return NULL;
//index + numero chars ultrapassa o tamanho da string original?
if(index + numchars > strlen(src))
return NULL;
for(i = index; i < (index + numchars); i++, n++)
buffer[n] = src[i];
return buffer;
}
este codigo foi escrito directamente na caixa de "reply" e não foi testado.
o meu conselho: se puderes usar C++, usa a class std::string que te safa duma data de dores de cabeça.
etraudpt 26-10-2006, 16:56 Em Basic:
InStr Function
Returns a Variant (Long) specifying the position of the first occurrence of one string within another.
Syntax
InStr([start, ]string1, string2[, compare])
etraudpt 26-10-2006, 16:58 Mid Function:
Returns a Variant (String) containing a specified number of characters from a string.
Syntax
Mid(string, start[, length])
Com um pouco de google...
http://www.cs.cf.ac.uk/Dave/C/node19.html#SECTION001911000000000000000
Aqui fica um pequeno preview deste link:
char *strchr(const char *string, int c) -- Find first occurrence of character c in string.
char *strrchr(const char *string, int c) -- Find last occurrence of character c in string.
char *strstr(const char *s1, const char *s2) -- locates the first occurrence of the string s2 in string s1.
char *strpbrk(const char *s1, const char *s2) -- returns a pointer to the first occurrence in string s1 of any character from string s2, or a null pointer if no character from s2 exists in s1
size_t strspn(const char *s1, const char *s2) -- returns the number of characters at the begining of s1 that match s2.
size_t strcspn(const char *s1, const char *s2) -- returns the number of characters at the begining of s1 that do not match s2.
char *strtok(char *s1, const char *s2) -- break the string pointed to by s1 into a sequence of tokens, each of which is delimited by one or more characters from the string pointed to by s2.
char *strtok_r(char *s1, const char *s2, char **lasts) -- has the same functionality as strtok() except that a pointer to a string placeholder lasts must be supplied by the caller.
http://www.cprogramming.com/tutorial/lesson9.html
Acho que o primeiro link já é uma grande ajuda :)
abraços, HecKel
Angelizer 26-10-2006, 20:22 eh bem pessoal obrigadao ai pela ajuda acho que ja me tou a safar...:D
so tou aki kom um prob faço #include <string.h> e ele nao me le as funçoes de strings que tou a usar...lol
dame um erro : "undefined reference to 'strcmp' " o.O
Em BASIC:
--> devolver o numero de caracters de uma string..
x = Len("Ola") -> retorna 3
--> determinar a posiçao de uma substring dentro de uma string
x = InStr(1, "Hello World", "l") -> retorna 3. Retorna 0 caso não encontre a String2 dentro da String1.
--> extrair uma string, apartir de uma posiçao, X caracters
x = Mid("Hello World", 3, 5) -> retorna "llo W". Usas como Mid(a String, Posição Inicial, [quantos bytes queres "extrair"]). O último argumento é facultativo, e caso o omitas, começa na posição inicial e "extrai" tudo o que está à frente (ficaria "llo World").
Espero ter ajudado :)
Cumps [[[[[[[[]]]]]]]]]
angelofwisdom
|
|