Thread: C Programming University Homework

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    6

    Question C Programming University Homework

    i just start before few week to learn c in university
    i have homework to send to the Thacher tomorrow and i stuck with problem and i don't find solution the quastion is that i need to get string and if 3 letter with ascii number one after one its need to be like this : if i get string : abc its need to beck a-c i just fix it and its work well now i need to fix if its 4 or 5 letter one agter one so if its 4 its should look like this get : abcd return a-d and if its 5 letter one after one its look like this abcde return a-e
    hope you understend what i mean i really will be thenfual if some one can help me
    this is the code i write until now :

    and this is the input: dabcefLMNOpQrstuv567zyz
    The final answer should look like this : da-cefL-OpQr-v567zyx


    with my code its return da-cefL-NOpQr-tuv-67zyx

    hope someone can help me

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define SIZE 80
    int change(char str[]);
    
        int main()
        {
            char str[SIZE];
            printf("Enter The String Please: \n");
            scanf("%s",str);
            printf("Your String is:  %s\n",str);
            change(str);
            return 0;
    
        }   
        
        int change(char str[])
        {
            int count=1;
            int i;
            
            
            int start;
            int end;
            int now=0;     
            for (i = 0; i <= strlen(str); i++)
            {              
                    if(((int)str[i]) == ((int)str[i+1]-1))
                     {
                        count++;
                        start = i;
                        end = i+1; 
                        printf("The number of change1 is: %d\n", count);
                        printf("Your new String is:  %s\n", str);
                       }  
                  if(count == 5)
                 { 
                 int k;
                    for(k=i; str[k] != '\0';k++)
                        {                                             str[k] = str[k+2];
                             printf("countrtttttt %d\n",count);
                             printf("Your new String is:  %s\n",str);
                             k--;
                        }
                    printf("The number of change5 is: %d\n", count);
                    printf("Your new String is:  %s\n", str);
                 } 
                  if(count == 4 )
                 {                  int k;
                    for(k=i; str[k] != '\0';k++)
                        {                                             str[end] = str[k+1];
                             printf("countrtttttt %d\n",count);
                             printf("Your new String is:  %s\n",str);
                             k--;
                        }
                        
                    printf("The number of change5 is: %d\n", count);
                    printf("Your new String is:  %s\n", str);
                 }   
                  if(count == 3)
                 {
                    now=(start+end)/2;
                    str[now] = '-';
                    count =0;
                    printf("The number of change is: %d\n", count);               
                    printf("Your new String is:  %s\n", str);
                 }
                
                
               
            }       
            printf("Your new String is:  %s\n", str);
            printf("Str look like this: da-cefL-OpQr-v567zyx \n");
            printf("The number of change is: %d\n", count);
            return 0;
        }
    Last edited by rezervi1; 04-07-2018 at 08:25 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    So if the user enters
    abcdejklmn
    your output should be
    a-ej-n
    ?

    You need to remove all those if(count == 5), ==4, ==3 specific tests. You can't go all the way up to count==26 like that.

    Work out what is common to all the code examples, and write generalised code just once.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    6
    Thanks for answer so fast .
    i just edit the code ans show what its show me now .
    still dont work well but im in the right side
    i try a lot to think how to write one code to all the option but i dont find it

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Here's something to think about.
    Code:
    #include<stdlib.h>
    #include<string.h>
    #include<stdio.h>
    int main ( ) {
      char test[] = "abcdejklmn";
      int start = 0;
      int end = strlen(test);
      for ( int i = start ; i < end ; i++ ) {
        for ( int j = i+1 ; j <= end ; j++ ) {
          if ( test[j-1]+1 == test[j] ) {
            printf("%c follows %c\n", test[j], test[j-1]);
          } else {
            printf("%c ends sequence starting at %c\n", test[j-1], test[i] );
            i = j-1;
            break;
          }
        }
      }
    }
    
    $ gcc foo.c
    $ ./a.out 
    b follows a
    c follows b
    d follows c
    e follows d
    e ends sequence starting at a
    k follows j
    l follows k
    m follows l
    n follows m
    n ends sequence starting at j
    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.

  5. #5
    Registered User
    Join Date
    Apr 2018
    Posts
    6
    thanks bu i dont find how to change it to my quastion !
    i think about somwthing new : but its not compile to me :

    Code:
     int change(char str[])
        {         
            int i=0;
            int j =1;
            int length = 0;
            int start;
            int end;
            
            while(str[i])
            {             
                if(isalpha(str[i]))
                {                
                    if(str[i] == str[j]-1)
                    {                     
                     start = i;                   
                     length++;
                       
                     end = i -length;
                      
                        if(length >= 2)
                        {                         
                        start = i;                         
                        str[i+1] = '-';                        
                        strcpy(str+i,end+i);
                                               
                        }                                   
      }
                    else
                    strcpy(str+1,str+j);
                    
                    
                                }
                                  i++;             
                                     }
            
                                 printf("Your String is:  %s\n",str);
                                 return 0;   
      }
    Last edited by rezervi1; 04-08-2018 at 08:12 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Does your code really look that messy in your code editor?

    Indentation style - Wikipedia
    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.

  7. #7
    Registered User
    Join Date
    Apr 2018
    Posts
    6
    sorry but its dont look like this in the gedit .
    i dont know why its not identation in the site sorry
    the new idea i got is to check every char if the next one is bit in one its count
    then its change the string in strcpy(str+1,str+length) only the first char and the last char in and if its not its print the char and contuntie one more .
    its not working me in the code bu i think this is the right way .
    did you have any idea how to write?
    Thanks For answer.
    Last edited by rezervi1; 04-08-2018 at 08:18 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    If you mix spaces and tabs for indentation, then invariably it will look a mess at some point.
    Make sure you just use spaces only.
    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. C programming homework
    By jonathan123456 in forum C Programming
    Replies: 2
    Last Post: 11-09-2017, 03:00 PM
  2. University level C programming course for free online
    By skyrocket1530 in forum C Programming
    Replies: 3
    Last Post: 11-21-2012, 08:03 PM
  3. new to c programming, Homework help
    By esardinha08 in forum C Programming
    Replies: 10
    Last Post: 01-01-2012, 10:42 PM
  4. Replies: 4
    Last Post: 10-31-2011, 03:14 AM
  5. What should be the Homework for game Programming?
    By mistu4u in forum Game Programming
    Replies: 7
    Last Post: 06-16-2011, 12:36 AM

Tags for this Thread