Thread: Re-Assigning Values To Array

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Windu102
    Guest
    Hi, i'm really new to (C) programming.
    I'm practicing by writing a code that will take a string, and then send the individual words of that string, to a function to be printed. The problem is that because i override the characters in the respective elements of the array, if one of the words is shorter than the one before it, it carries across the excess letter.

    E.g "what you want" gives me :
    what you want
    what
    yout
    want

    I'm thinking i need to re assign the value 0 to my whole array (since apparently i cant re-initialize in C), problem is the loop I'm using doesn't work. When i compile it in Visual C++, i get the following message.

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

    You'll see the loop commented out in the second If statement in my While.

    Warning. What your about to see is a very ugly, and extremely inefficient chunk of code. Please be gentle.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(int argc, char *argv[]){
    
    char mytext[21] = {"what you want"};
    char textblock[21] = {0};
    int wordplace(char textblock[]);
    
    printf("%s\n", mytext);
    
    int i = 0;
    int j = 0;
    int f = 0;
    
    while((i<22) && (j<22) && (mytext[i] != '\0')){
    	
    	if(( mytext[i]>='a' && mytext[i]<='z' ) || ( mytext[i]>='A' && mytext[i]<='Z' )){
           textblock[j] = mytext[i];
    	}
    
    	i++;
    	j++;
    
    	if (mytext[i] == ' ' || mytext == 0 || mytext[i] == '\0'){ 
    		mytext[i] = '\0';
    		wordplace(textblock);
    		i++;
    		j = 0;
    		//while (j<22){
    	       //textblock[j] = 0;
    		   //j++;
    		}
    		
    	}
    
    }
    
    return 0;
    
    }
    
    int wordplace(char textblock[]){
    
    	printf("%s\n", textblock);
    
    	return 0;
    
    }

    Please help. Thank You.
    Last edited by Windu102; 08-21-2008 at 08:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 06:24 AM
  2. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Duplicate values in Array
    By TONYMX3 in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2002, 03:57 PM