Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Passing Strings && randomizing

c string

  • Please log in to reply
14 replies to this topic

#1 Pally

Pally

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts
  • Programming Language:C, Java, PHP, Python, PL/SQL, Assembly
  • Learning:C++, C#, Lisp, Scheme

Posted 22 October 2012 - 05:23 PM

Thanks for your time,

how to go about moving a string to this function and then scrambling it... here is what I have (language C)

the form has to be as...



void scramble(char *string){

}


void main(){

}



thus I've tried about everything I can thing off... but heres my current




void scramble(char *string){
char copy[sizeof(string)];
strcpy(copy, string);
}


void main(){
	char *words[5] = { "pumpkin", "cantalope", "watermelon", "apple", "kumquat" };
	scramble(words[0]);
}

then strcpy makes an error.. but I'm trying to make it so I can get everything into characters then i'll be able to switch stuff around

thanks
  • 0
Your Friendly Neighborhood Pally

#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 22 October 2012 - 09:51 PM

What error does strcopy cause?
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#3 Pally

Pally

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts
  • Programming Language:C, Java, PHP, Python, PL/SQL, Assembly
  • Learning:C++, C#, Lisp, Scheme

Posted 23 October 2012 - 04:35 AM

What error does strcopy cause?


Run-Time Check Failure #2 - Stack around the variable 'copy' was corrupted.


I think it has to do with converting it from a char* to a char... but idk how else I can modify its contents without doing so... I'm sitting on my ** last few days can't get anywhere with this


Problem is I can't figure out how to get a string pointer into a char array, if I'm saying that correctly
  • 0
Your Friendly Neighborhood Pally

#4 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 23 October 2012 - 05:41 AM

This error usually means you are trying to put more data than the destination can hold. At first glance, you are checking the size of the pointer.
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#5 Pally

Pally

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts
  • Programming Language:C, Java, PHP, Python, PL/SQL, Assembly
  • Learning:C++, C#, Lisp, Scheme

Posted 23 October 2012 - 07:55 AM

This error usually means you are trying to put more data than the destination can hold. At first glance, you are checking the size of the pointer.


heres what I got right now.. I'll just leave fake // comments next to my problems... thank you


#include <string.h>
#include <stdio.h>


void scramble(char *string){
char copy[8]; // well what if I send a bigger or smaller word.. then also I'd be scrambling white spaces up as well <img src='http://img.codecall.net/public/style_emoticons/<#EMO_DIR#>/sad.png' class='bbc_emoticon' alt=':(' />
strcpy(copy, string);
printf("\n\nprint: %s", copy); // ok.. but then copy[0] doesn't work or I get a error.. thus I can't really access the individual letters it appears?
}

void scrambleGame(char *original, char *scrambled){

}

void trim(char *string){

}

void main(){
char *words[5] = { "pumpkin", "cantalope", "watermelon", "apple", "kumquat" };

scramble(words[0]);

}

Edited by dargueta, 23 October 2012 - 10:02 AM.
Please use code tags!

  • 0
Your Friendly Neighborhood Pally

#6 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 23 October 2012 - 08:05 AM

2 of your words are larger than 8 characters.
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#7 Pally

Pally

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts
  • Programming Language:C, Java, PHP, Python, PL/SQL, Assembly
  • Learning:C++, C#, Lisp, Scheme

Posted 23 October 2012 - 08:10 AM

2 of your words are larger than 8 characters.


yes I've made note of that above.. thats why I tried char copy[sizeof(string)];

but thats had issues.. everything I've tried has issues.. basically going no where with this thing

  • 0
Your Friendly Neighborhood Pally

#8 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 23 October 2012 - 09:56 AM

do a quit test to hopefully explain what I'm saying.

printf("the size of %s is %d and the length is %d\n\n", string, sizeof(string), strlen(string));
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#9 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 23 October 2012 - 10:03 AM

Use strlen(string) + 1, not sizeof(string).
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#10 Pally

Pally

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts
  • Programming Language:C, Java, PHP, Python, PL/SQL, Assembly
  • Learning:C++, C#, Lisp, Scheme

Posted 23 October 2012 - 10:42 AM

Use strlen(string) + 1, not sizeof(string).


ok makes a little more since now, but still doesn't work

won't let me do

char copy[strlen(string)+1];

it gives an error "expected constant expression"


if I can't do this.. then I can't allocate a perfectly size array to the incoming string to scramble it.. but I feel like I should be able to ?


if not I'll just have to make a big array whatever works I guess?



I also tried this... but gives same error




const int length = strlen(string)+1;
char copy[length];
  • 0
Your Friendly Neighborhood Pally

#11 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 23 October 2012 - 11:32 AM

This is C. You can't declare strings of arbitrary (non-constant) length on the stack with:
char copy[strlen(string)+1]; // C/C++ expects a const value between the [].

You must declare a string pointer and then use malloc() to allocate your memory on the heap.

Example:
char* copy = (char*) malloc(strlen(string)+1);
...
free(copy); // Don't forget to free up any allocated memory when you're done with it!

  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#12 Pally

Pally

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts
  • Programming Language:C, Java, PHP, Python, PL/SQL, Assembly
  • Learning:C++, C#, Lisp, Scheme

Posted 23 October 2012 - 11:40 AM

This is C. You can't declare strings of arbitrary (non-constant) length on the stack with:

char copy[strlen(string)+1]; // C/C++ expects a const value between the [].

You must declare a string pointer and then use malloc() to allocate your memory on the heap.

Example:
char* copy = (char*) malloc(strlen(string)+1);
...
free(copy); // Don't forget to free up any allocated memory when you're done with it!



Ok I got it working, atleast the first part that I need.. now theres 7 other things I need to implement :D

heres what I did



void scramble(char *string){
char *scramble= (char *) malloc(strlen(string)+1);
char temp;
int num1,num2, i;

strcpy(scramble, string);

srand(time(NULL));

for(i = 0;i<=200;i++){ //here I'll randomize and scramble word
num1 = rand()*rand()%(strlen(string));
num2 = rand()*rand()%(strlen(string));
printf("num= %d",num1);
temp = scramble[num1];
scramble[num1] = scramble[num2] ;
scramble[num2] = temp;
}

printf("\n\nprint: %s", scramble);
}




Thank you!

Edited by pally98, 23 October 2012 - 11:57 AM.
Please use [CODE][/CODE] tags!

  • 0
Your Friendly Neighborhood Pally





Also tagged with one or more of these keywords: c, string

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download