I am trying to convert a vector into a string using the following function.
char* my_vect2str(char** input)
{
int i;
char* ret = (char*)xmalloc(sizeof(char*));
for(i=0; input[i] != NULL; i++)
{
if(*input[i] == '\0')
ret[i] = ' ';
else
ret[i] = *input[i];
}
ret[i] = '\0';
return ret;
}
This appears to be getting just the first character of each string in the vector. How do I alter my for loop to get this working properly? Thanks!
Your malloc should be the size of the pointer contents, not the pointer itself. You also don't need to cast the malloc void *. You need an inner loop counter in order to iterate through both dimensions of you pointer. This should work:
char* my_vect2str(char** input)
{
int i;
int count = 0;
char* ret = (char*)malloc(sizeof(char*)); // should be a larger size
for(i=0; input[i] != NULL; i++)
{
int j = 0;
while(1){
if(input[i][j] == '\0'){
ret[count++] = ' ';
break;
}else{
ret[count++] = input[i][j];
}
j++;
}
}
ret[count] = '\0';
return ret;
}
The first loop calculates the total size of the strings in input. Then, the space is allocated and the strings are concatenated to ret.
char* my_vect2str(char** input)
{
int i, j, k = 0;
char* ret;
int size = 0;
int len;
char* inp = input[k++];
while (inp != NULL) {
size += strlen(inp);
inp = input[k++];
}
ret = malloc((size * sizeof(char)) + 1);
memset(ret, 0, size + 1);
i = 0;
j = 0;
while (i < size) {
if (input[j] != NULL) {
len = strlen(input[j]);
memcpy(&ret[i], input[j], len);
i += len;
}
++j;
}
return ret;
}
Related
I recently started learning basic C and I am still noobie with it, I started doing some projects for learning and I am using library functions but I am interested in other methods...
So I have an email validation, it works fine, but I want to do it without strlen, any suggestion what can I do instead of strlen?
void mail(char e[]) {
int count = 0;
int countb = 0;
int i, j;
int t, t2;
int p = 0;
for (i = 0; i < strlen(e); i++) {
if (e[i] == '#') {
count++;
t = i;
}
}
if (count == 1) {
for (j = 0; j < t; j++) {
if (!(e[j] == '_' || e[j] == '.' || isalpha(e[j]) || isdigit(e[j]))) {
p = -1;
printf("\nwrong\n");
break;
}
}
if (p == 0) {
for (i = t; i < strlen(e); i++) {
if (e[i] == '.') {
t2 = i;
countb++;
}
}
if (countb == 1) {
for (i = 0; i < t2 && i > t2; i++) {
if (!(isalpha(e[i]))) {
p = -1;
printf("\nwrong\n");
break;
} else {
p = 1;
}
}
if (p == 1) {
if (e[t2 + 3] != '\0') {
p = -1;
printf("\nwrong\n");
}
}
} else {
p =- 1;
printf("\nwrong\n");
}
}
} else {
p = -1;
printf("\nwrong\n");
}
return;
}
Instead of comparing i < strlen(e) you can test if the byte at offset i is not the null terminator: e[i] != '\0'.
You can also compute the length of the string just once at the beginning of the function and store it into a len variable.
The only thing you can do is to implement your own strlen function.
In C, strings are null-terminated char arrays, that means, the last character of an array is '\0' (zero). The strlen function iterates over the character array until a null-character is found, it returns the length of that array, the null-character not included.
To duplicate (create) a c-string, you have to do the following:
char *str = malloc(strlen(another_string) + 1);
str[strlen(another_string)] = '\0';
Example for strlen:
size_t my_strlen(const char *str)
{
if (!str) return 0;
size_t n = 0;
for (; *str; ++n, ++str);
return n;
}
You can create you own my_strlen :)
unsigned int my_strlen(char *str)
OR
you can loop while your char is different of '\0' (null byte). So just replace strlen(e) with e[i] != '\0'
It should work just fine
Please, help with the code.
Requirement:
Write a function my_union that takes two strings and returns, without doubles, the characters that appear in either one of the strings.
Example:
Input: "zpadinton" && "paqefwtdjetyiytjneytjoeyjnejeyj"
Output: "zpadintoqefwjy"
My code:
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
char *my_union(char *a, char *b) {
char *str;
// Algorithm for excluding nonunique characters from string a(given in
// parameters).
str[0] = a[0];
int k = 1;
str[k] = '\0';
for (int i = 1; a[i] != '\0'; i++) {
bool is = true;
for (int j = 0; str[j] != '\0'; j++) {
if (str[j] == a[i]) {
is = false;
break;
}
}
if (is) {
str[k] = a[i];
k++;
str[k] = '\0';
}
} // In this case we are excluding excess character 'n' from "zpadinton", so
// str is equal to "zpadinto".
// Algorithm for adding unique characters from array b(given in parameters)
// into str.
for (int i = 0; b[i] != '\0'; i++) {
bool is = true;
for (int j = 0; str[j] != '\0'; j++) {
if (str[j] == b[i]) {
is = false;
break;
}
}
if (is) {
strncat(str, &b[i], 1);
}
}
return str;
}
The first algorithm is almost identical with second, but it doesn't work(. Mb I messed up with memory, give some advice, pls.
If you mean, get the unique characters from two strings and store them into a new string, try this code ;
First, you must allocate a memory for str. In your code, str is not pointing allocated memory location, so you will probably get segmentation fault.
int contains(const char * str,char c)
{
for (int i = 0; i < strlen(str); ++i)
if(str[i] == c)
return 1;
return 0;
}
char * my_union(char *a, char*b)
{
char * res = (char*)malloc(sizeof(char)*(strlen(a) + strlen(b)));
int pushed = 0;
for (int i = 0; i < strlen(a); ++i)
{
if(!contains(res,a[i])){
res[pushed] = a[i];
pushed++;
}
}
for (int i = 0; i < strlen(b); ++i)
{
if(!contains(res,b[i])){
res[pushed] = b[i];
pushed++;
}
}
return res;
}
int main(int argc, char const *argv[])
{
char string1[9] = "abcdefgh";
char string2[9] = "abegzygj";
char * result = my_union(string1,string2);
printf("%s\n", result);
return 0;
}
Also, do not forget the free the return value of my_union after you done with it.
I am supposed to save every sequence of digits from a string in an array of chars , this is what i tried:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int check_number(char *s) {
for (; *s; ++s) {
if (!isdigit(*s))
return 0;
}
return 1;
}
void int_in_string(char *s, char **ar, int MaxCap) {
char temp[100];
int index = 0;
int i = 0;
for (; *s; s++) {
if (index == MaxCap) {
break;
}
if (isdigit(*s)) {
temp[i++] = *s;
}
if (*s == ' ' && check_number(temp)) {
ar[index++] = temp;
memset(temp, '\0', i);
i = 0;
}
}
if (index == 0) {
printf("no numbers in string");
}
for (int i = 0; i < index; i++)
printf(" %s \n", ar[i]);
}
but this code only prints several newlines , can someone explain me what i do wrong?
Some issues:
ar[index++]=temp;
This is just storing the same value (the address of temp) over and over. What you need to do is copy the string into the array.
Also, you need to terminate the string temp with '\0'. You handle this in all but the first string with memset(temp, '\0', i); However, since local variables are not initialized, you need to do it:
char temp[100] = {0}
Or, you can remove the initialization and the memset by just adding the EOS:
temp[i] = '\0';
Lastly, since you declare the original array as
char * ar[10];
You are not allocating any space for the strings. The simplest way to handle that is with strdup.
void int_in_string(char *s, char **ar, int MaxCap)
{
char temp[100];
int index = 0;
int i = 0;
for (; *s; s++) {
if (isdigit(*s)) {
temp[i++] = *s;
// Need to avoid buffer overflow
if (i == sizeof(temp)) {
i = 0;
}
}
if (isspace(*s)) {
temp[i] = '\0';
// strdup will allocate memory for the string, then copy it
ar[index++] = strdup(temp);
// if (NULL == ar[index-1]) TODO: Handle no memory error
i = 0;
if (index == MaxCap) {
break;
}
}
}
if (index == 0) {
printf("no numbers in string");
}
for (int i = 0; i < index; i++) {
printf(" %s \n", ar[i]);
// free the mem from strdup
free(ar[i]);
}
}
I believe some systems may not have strdup(). If not, it can be easily replicated:
char * my_strdup(const char *src)
{
if (src == NULL) return NULL;
char *dest = malloc(strlen(src) + 1);
if (dest == NULL) return NULL;
strcpy(dest, src);
return dest;
}
I want to count the number of words that are in a file. I store each line of the text in the file using a double pointer and then manipulate it do other things.
char **create2DArray()
{
int i = 0;
char **str = malloc(sizeof(char *) * 100);
for (i = 0; i < 100; i++)
{
str[i] = malloc(sizeof(char) * 1000);
}
return str;
}
char **readFile(char **str)
{
int i = 0;
FILE *pFile;
char *filename = "C:\\Users\\muham\\OneDrive\\Documents\\A2\\A2 Samples\\sample1.txt";
pFile = fopen(filename, "r");
if (pFile == NULL)
{
printf("Could not open file");
exit(1);
}
while (fgets(str[i], 1000, pFile) != NULL)
{
RemoveReturn(str[i]);
lineCount++;
printf("%s\n", str[i]);
i++;
}
fclose(pFile);
return str;
}
int wordCount(char **str)
{
int wordCounting = 0;
int i = 0;
int q = 0;
for (i = 0; i < lineCount; i++)
{
for (q = 0; q <= strlen(str[i]); q++)
{
if (*str[q] == ' ' || *str[q] == '\0')
{
wordCounting++;
}
if (*str[q] == ' ' && *str[q + 1] == ' ' && *str[0] != ' ')
{
wordCounting--;
}
if (*str[0] == ' ')
{
wordCounting--;
}
if (*str[q] == ' ' && *str[q + 1] == '\0')
{
wordCounting--;
}
if (strlen(str[q]) == 0)
{
wordCounting--;
}
}
}
printf("%d\n", wordCounting);
return wordCounting;
}
As of right now, when I run the program, wordCount prints 0. Why is this happening? Is it because I am iterating through the number of pointers with str[i] and not the strings stored in str[i]? How do I fix this?
There are several issues in your code; The most obvious one is probably your loop for (i = 0; i <= strlen(str[i]); i++), in which you compare the length of the ith string with the value of i, and you use the same i then to access the characters of the ith string. This all rarely makes sense.
I'd start with two things:
First, make sure that you do not access uninitialized rows, i.e. consider lineCount. A simple way would be to make it either a global variable or to return it in readFile; signature would change to int readFile(char **str) { ....; return lineCount; }
Second, use two nested loops:
for (int line=0; line<lineCount; line++) {
for (int column=0; column < strlen(str[line]); column++) {
// your code for detecting lines goes here...
}
}
I have been trying to figure out how to modify an array of char pointers but no matter what I do there appears to be no change below are the three arrays I'm trying to change including the call to the function I'm using.
char*cm1[5];
char*cm2[5];
char*cm3[5];
setupCommands(&cm1,commands,file,0);
setupCommands(&cm2,commands,file,1);
setupCommands(&cm3,commands,file,2);
The code below is the function itself.I was thinking that maybe it involves a double pointer but if I try *cmd to change the array I get a segmentation fault.
void setupCommands(char **cmd[], char* commands[],char file[],int index){
char str1[255];
strcpy(str1,commands[index]);
char newString [5][255];
int j = 0;
int ctr = 0;
int i;
//printf("str1 %s\n" ,str1);
for(i = 0; i <= strlen(str1); i++){
if(str1[i] == ' '|| str1[i] =='\0'){
newString[ctr][j] = '\0';
ctr++;//next row
j=0;// for next word, init index to 0
}else{
newString[ctr][j]=str1[i];
j++;
}
}
for(i = 0; i < ctr; i++){
//printf(" test2 %s \n", newString[i]);
cmd[i] = newString[i];
//printf(" test2 %d %s \n", i,cmd[i]);
}
//printf("index %d", i);
cmd[i]= file;
cmd[i + 1] = NULL;
//execvp(cmd[0],cmd);
//cmd
}
There are a few issues with your code:
you are trying to return references to the local 'char newString [5][255]' when the function exits. In simple worlds - never return anything locally allocated on the stack. This is the reason you are getting the segmentation fault.
char **cmd[] must be declared char *cmd[] - even though you will get a warning from the compiler assignment from incompatible pointer type, the code would run and execute correctly(essentially **cmd[] would do the same work as *cmd[], even though it's not of correct type) if you didn't return references to the local object;
Easy and simple optimization is just to remove the array str1 and directly operate on the array commands.
Apart from this simple optimization I have changed your code to overcome the segmentation fault, by allocating on the heap, instead on stack(will live until the program terminates) the multidimensional array, and I also calculate it's size so I will know how much memory to allocate. Now it's safe to return references to it.
Note that more optimizations could be made, but for the sake of the simplicity this is the bare minimal for this code to work.
int setupCommands(char *cmd[], char *commands[], char file[], int index)
{
int j = 0;
int ctr = 0;
int i = 0;
int rows = 0;
int cols = 0;
char **newString = NULL;
while(commands[index][i])
{
if (commands[index][i] == ' ')
{
++rows;
}
++i;
}
++rows;
cols = strlen(commands[index]) + 1;
newString = malloc(rows * sizeof(*newString));
if (newString == NULL)
{
return -1;
}
for (i = 0; i < rows; ++i)
{
newString[i] = malloc(cols * sizeof(*newString));
if (newString[i] == NULL)
{
return -1;
}
}
for(i = 0; i <= strlen(commands[index]); i++){
if(commands[index][i] == ' '|| commands[index][i] =='\0'){
newString[ctr][j] = '\0';
ctr++;//next row
j=0;// for next word, init index to 0
}else{
newString[ctr][j]=commands[index][i];
j++;
}
}
for(i = 0; i < ctr; i++){
cmd[i] = newString[i];
}
cmd[i]= file;
cmd[i + 1] = NULL;
return 0;
}
First of all - being the three stars pointer programmer is not good :)
You assign it with pointer to the local variable which is not longer available after the function return
But if you still want the three stars pointers:
char **cm1;
char **cm2;
char **cm3;
setupCommands(&cm1,commands,file,0);
setupCommands(&cm2,commands,file,1);
setupCommands(&cm3,commands,file,2);
#define MAXWORD 256
int setupCommands(char ***cmd, const char *commands,const char *file,int index){
char str1[255];
strcpy(str1,commands[index]);
int j = 0;
int ctr = 0;
int i;
//printf("str1 %s\n" ,str1);
*cmd = malloc(sizeof(char *));
**cmd = malloc(MAXWORD);
if(!*cmd || !**cmd)
{
/* do spmething if mallocs failed*/
return -1;
}
for(i = 0; i <= strlen(str1); i++){
if(str1[i] == ' '|| str1[i] =='\0'){
(*cmd)[ctr][j] = '\0';
ctr++;//next row
*cmd = realloc((ctr + 1) * sizeof(int));
(*cmd)[ctr] = malloc(MAXWORD);
if(!*cmd || !*cmd[ctr])
{
/* do spmething if mallocs failed*/
return -1;
}
j=0;// for next word, init index to 0
}else{
(*cmd)[ctr][j]=str1[i];
j++;
}
}
*cmd = realloc(sizeof(char *) * ctr + 2)
(*cmd)[ctr - 2] = malloc(MAX);
if(!*cmd || !*cmd[ctr - 2])
{
/* do spmething if mallocs failed*/
return -1;
}
strcpy((*cmd)[ctr - 2], file);
(*cmd)[ctr - 1] = NULL;
return 0;
//execvp(cmd[0],cmd);
//cmd
}
you can improve many things (for example do not realloc every time but in the larger chunks) and I did not change anything in your code logic.