Thread: Adding a string with another string to get the output of the letters added?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    23

    Adding a string with another string to get the output of the letters added?

    Hello, i was wondering how i would add the char of a string to the char of another string... so i can print out each of these. It is for a cypher so it would look like this...
    HELLO
    +ABABA
    =I GMNP

    so basically the value of the one added on away from 'A' is how much is added.
    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You can use the numeric value of the characters, i.e. 'A' = 41 (assuming ASCII). Then just loop through the strings one character at a time and add them together. The only special case is if char1 + char 2 > 'Z'.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    If string1 = "HELLO" and string2="ABABA" then
    Subtract 'A' from every letter in string2; ABABA becomes 12121
    Add the result to string1; H+1 == I, E+2 == G, L+1 == M, etc..
    Check if string1[current char] > 'Z', then subtract 'Z'-'A'

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    Quote Originally Posted by _Mike View Post
    If string1 = "HELLO" and string2="ABABA" then
    Subtract 'A' from every letter in string2; ABABA becomes 12121
    Add the result to string1; H+1 == I, E+2 == G, L+1 == M, etc..
    Check if string1[current char] > 'Z', then subtract 'Z'-'A'
    I tried to do this but it did not work... i'm getting weird chars that are unknow symbols?

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    okay so this is what i've got it's not working for me. any help would be appreciated. Thanks

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    
          int
    main (int argc, char *argv[]){
    	int i, j, len_key;
    	  char c;	  
    
    	  argv[1][3]='\0';
    
    	  len_key=strlen(argv[1]);
    
    	  c=getchar();
    	  for(j=0; c!='\n' && c!=EOF; c=getchar()){
    	    if(j>len_key){
    	      j=0;
    	    }
    	    argv[1][j]=argv[1][j]-'a'+1;
    	    printf("%c", c+argv[1][j]);
    	    j++;
    	  }
    
    	  if(argc==1){
    	    printf("usage: %s [ -d ] key\n", argv[0]);
    	    return 0;
    	
    	return 0;
          }

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Creedy View Post
    okay so this is what i've got it's not working for me. any help would be appreciated. Thanks
    Code:
    argv[1][3]='\0';
    why are you doing this?

    Code:
    if(j>len_key){
        j=0;
    }
    You should test for == or >= because you don't want to process the null terminator.
    Code:
    argv[1][j]=argv[1][j]-'a'+1;
    I don't know if modifying argv's contents are legal or not. You should probably copy it to a temporary string just to be safe.

    Code:
    if(argc==1){
        printf("usage: %s [ -d ] key\n", argv[0]);
        return 0;
    Missing closing bracket. And you might want to check if the arguments are valid before you process them, not after.
    Plus the usage example is missleading. If the user runs the app with
    ./appname -d key
    then "-d" is used as the key.

    Edit:
    Forgot to say,
    in your for loop; if the key is shorter than the string inputted in the loop then you are subtracting 'a' from the key more than once, which is why you are getting weird output.
    Last edited by _Mike; 03-02-2010 at 07:47 PM.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    I fixed the argv null issue.
    Also I fixed the j greater than equal to lenkey.
    It still doesn't work and Im not sure what you mean about where you edited.
    because j is increasing.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Creedy View Post
    It still doesn't work and Im not sure what you mean about where you edited.
    because j is increasing.
    If you have "hello" as input and "ab" as key then this is what your loop does:

    Code:
    set j to 0
    subtract 'a' from (key[j]+1). key[0] = 1
    print input[0]+key[0]. ('h'+1, prints 'i')
    
    set j to 1
    subtract 'a' from (key[j]+1). key[1] = 2
    print input[1]+key[1]. ('e'+2, prints 'g')
    
    set j to 0
    subtract 'a' from (key[j]+1) // this is where things go wrong, because key[0] has already been modified once before
    print input[2]+key[0]. (prints garbage)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 12:18 PM
  2. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM