char* dynamicallyScanString(char **str) {
char *pStr = *str;
}

This will not work any help is appreciated!

Edit :
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

char *toLowerCase(char *str) {
int i;
if(str[i]>=65&&str[i]<=90){
str[i]=str[i]+32;
}
printf("%s", str);
return str;
}

void doIt(char *str) {
str = toLowerCase(str);
printf("%s", str);
}

char* dynamicallyScanString(char **str) {
int len = 16;
int currentLen = 0;
char *pStr;
pStr = *str;
pStr = (char*)malloc(len);
len = currentLen;
int c = EOF;
int i=0;
while((c = getchar()) != '\n' && c != EOF) {
/*if (getchar() == "\b") {
pStr[i] = '\0';
if (i > 0) { i--; }
} else {
pStr[i++] = (char)c;
} */
pStr[i++] = (char)c;
if (i == currentLen) {
currentLen = i + len;
pStr = realloc(str, currentLen);
}
}
//pStr[i] = '\0';
return pStr;

}

int main() {
printf("Hello, World!\n");
char *x;
//char *x = malloc(sizeof(char) * 32);
//scanf("%s", x);
//strcpy(x, "Hello");
dynamicallyScanString(&x);
//toLowerCase(x);
printf("%s", x);
//free(x);
return 0;
}
Insufficient information. That compiles (at least if you return something from that function) so as far as you've said it does work.
Tari wrote:
Insufficient information. That compiles (at least if you return something from that function) so as far as you've said it does work.



Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

char *toLowerCase(char *str) {
    int i;
    if(str[i]>=65&&str[i]<=90){
        str[i]=str[i]+32;
    }
    printf("%s", str);
    return str;
}

void doIt(char *str) {
    str = toLowerCase(str);
    printf("%s", str);
}

char* dynamicallyScanString(char **str) {
    int len = 16;
    int currentLen = 0;
    char *pStr;
    pStr = *str;
    pStr = (char*)malloc(len);
    len = currentLen;
    int c = EOF;
    int i=0;
    while((c = getchar()) != '\n' && c != EOF) {
        /*if (getchar() == "\b") {
            pStr[i] = '\0';
            if (i > 0) { i--; }
        } else {
            pStr[i++] = (char)c;
        } */
        pStr[i++] = (char)c;
        if (i == currentLen) {
            currentLen = i + len;
            pStr = realloc(str, currentLen);
        }
    }
    //pStr[i] = '\0';
    return pStr;

}

int main() {
    printf("Hello, World!\n");
    char *x;
    //char *x = malloc(sizeof(char) * 32);
    //scanf("%s", x);
    //strcpy(x, "Hello");
    dynamicallyScanString(&x);
    //toLowerCase(x);
    printf("%s", x);
    //free(x);
    return 0;
}
I see many issues with the code, but it compiles without errors, so you'll have to be more specific as to what problem you want to be fixed, and more importantly, what you want it to do in the first place.
jacobly wrote:
I see many issues with the code, but it compiles without errors, so you'll have to be more specific as to what problem you want to be fixed, and more importantly, what you want it to do in the first place.


I'm sorry that I wasn't specific enough. The program dynamically reallocates the string so as it gets larger there is more memory allocated to it. If you try to use the program and type a string it should print the string again. It will not print the string. Also what are the other problems with my code? Sorry again I am new at this.
atrt7 wrote:
I'm sorry that I wasn't specific enough. If you try to use the program and type a string it should print the string again. It will not print the string. Also what are the other problems with my code? Sorry again I am new at this.


The changes to pStr are strictly local to the function so they cannot affect the value of x in main, unless you assign to *str in dynamicallyScanString or use the return value of dynamicallyScanString in main to assign to x.
The changes to pStr are strictly local to the function so they cannot affect the value of x in main, unless you assign to *str in dynamicallyScanString or use the return value of dynamicallyScanString in main to assign to x.[/quote]

Wow I can't believe I didn't think of that thank you! Also is my code pretty much spaghetti code?
Well there's a function that does what you are trying to do called readline. If you can use it on whatever platform you are using, it would look something like this:

Code:
#include <stdio.h>
#include <stdlib.h>

int main() {
    // read the string
    char *str = NULL;
    size_t n = 0;
    ssize_t length = getline(&str, &n, stdin);
    if (length > 0 && str[length - 1] == '\n') {
        // remove the newline
        str[length - 1] = '\0';
    }

    // print the string
    fprintf(stdout, "%s\n", str);

    // free the string
    free(str);
    return 0;
}
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement