Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 19th, 2012  |  syntax: None  |  size: 0.64 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C - expanding the contents of a string
  2. #include <stdio.h>
  3.  
  4. void expand(char s1[], char s2[]);
  5.  
  6. int main() {
  7.  
  8.     char s1[] = "Talha-z";
  9.     char s2[] = "";
  10.  
  11.     expand(s1, s2);
  12.     printf(s2);
  13.  
  14. }
  15.  
  16. void expand(char s1[], char s2[]) {
  17.     int i = 0;
  18.     int j= 0;
  19.     int k, c_next;
  20.  
  21.     while ( s1[i] != '') {
  22.         switch (s1[i]) {
  23.         case ('-') :  
  24.             c_next = s1[i+1];
  25.             for ( k = 1; k < c_next; k++) {
  26.                 s2[j] = s1[i] + k;
  27.                 j++;
  28.             }
  29.             break;
  30.         }
  31.  
  32.         i++;
  33.         j++;
  34.     }
  35.     s2[j] = '';
  36. }
  37.        
  38. char s2[] = "";
  39.        
  40. char s2[1] = { '' };
  41.        
  42. s2[j] = ...