Thread: How to assign array to an array variable

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    92

    How to assign array to an array variable

    Problem text :

    Load a string ,which certainly can't be longer than 30 characters . If the loaded series
    contains some characters other than uppercase and lowercase letters of the English alphabet and digits 0-9 , print
    Message : loaded string not properly entered.
    If an array is properly entered, it is necessary to amend a series so that the lowercase a, e , and substitute with
    numbers 9,8,7 in the order given .
    Print original , and the resulting string .

    For example , the input string UlazniNizOd20Znakova prints:


    UlazniNizOd20Znakova Ul9zn7NIZOd20Zn9kov9

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    
    int main(){
    	
    	char string[30+1];
    	int i;
    	
    	scanf("%s", &string);
    	
    	for( i = 0; string[i] != '\0'; i++){
    		
    		if( !(string[i] >= '0' && string[i] <= '9'  || string[i] >= 'a' && string[i] <= 'z' || string[i] >= 'A' && string[i] <= 'Z') ){
    			printf("Loaded string is not properly entered.");
    		}
    		
    	}
    	char changedString[30+1] = string; 
    	
    	for( i = 0; i < 31; i++){
    		
    		if(string[i] == 'a'){
    			string[i] = '9';	
    		}else if(string[i] == 'b'){
    			string[i] = '8';
    		}else if( string[i] == 'c'){
    			string[i] = '7';
    		}
    	}
    	printf("Unchanged string: %s\n", string);
    	printf("Changed string: %s", changedString);
    
    
    }
    How to assign array to an array variable,
    and why my approach won't work ( guess it has to do something with adresses of the variables)
    I assume that in this kind of problems we use pointers but I just started learning about the pointers so I am not so practical with using them yet.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    scanf("%s", &string);
    should have been:
    Code:
    scanf("%30s", string);
    Remember, when passed as an argument, an array is converted to a pointer to its first element. Furthermore, the 30 in the format specification avoids buffer overflow. You should also check the return value of scanf.

    Next:
    Code:
    char changedString[30+1] = string;
    You can initialise an array with an initialiser, but not with another array. You can assign to the elements of an array of non-const elements, but you cannot assign to an array. Therefore, you should do something like:
    Code:
    char changedString[30+1] = "";
    strncat(changedString, string, 30)
    Though you should then #include <string.h>
    Last edited by laserlight; 05-27-2016 at 07:45 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by laserlight View Post
    Code:
    char changedString[30+1] = "";
    strncat(changedString, string, 30)
    When initializing array variable 'changedString[30+1]' why do I have to put "" sign ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Firstly, initialisation is different from assignment.

    Secondly,
    Code:
    char s[] = "hello";
    is just a syntactic sugared version of
    Code:
    char s[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign values 2d array to normal array
    By Gertjan Haan in forum C Programming
    Replies: 1
    Last Post: 10-29-2015, 11:10 PM
  2. how do I assign a char array to an int array?
    By mark 555 in forum C Programming
    Replies: 7
    Last Post: 12-13-2012, 10:24 PM
  3. Replies: 4
    Last Post: 05-06-2012, 06:24 AM
  4. Replies: 4
    Last Post: 07-01-2009, 01:53 PM
  5. Trying to assign array values to another array
    By edhc44 in forum C++ Programming
    Replies: 10
    Last Post: 02-01-2008, 10:26 PM

Tags for this Thread