How can i stamp strings in c without errors?

Daivy03

Write a program (in C) that asks for the IP address in a human readable form, creates three substrings, and prints them.

These substrings are created from parts 3, 2, and 1 of the last part of the IP address.

These substrings should be created with the use of pointers or array indexing (whichever you prefer). If a given string doesn't have three dots, then the program should print the message: Error: not a valid address..

Your program should also check if a given string consists only of digits and dots, and that there are no more than three digits in each block. You don't have to check if the numbers are smaller than 256.

Your version of the program must print the same result as the expected output.

I write this code below, but isn't working:

  #include <stdio.h>
  #include <stdlib.h>
  #include <math.h>
  #define LUNG 30
  #define INTLUNG 30



void controlla(char *str){
    char pt1[INTLUNG],pt2[INTLUNG],pt3[INTLUNG],pt4[INTLUNG];
    int i=0,j=0;
    while(str[i] !='.' && str[i] != '\0')
    {
        pt1[j]=str[i];
        i++;
    
    }
    i++;
    while(str[i] !='.' && str[i] != '\0')
    {
        pt2[j]=str[i];
        i++;
        j++;
    }
    j=0;
    i++;
    while(str[i] !='.' && str[i] != '\0')
    {
        pt3[j]=str[i];
        i++;
        j++;
    }
    j=0;
    i++;
    while(str[i] !='.' && str[i] != '\0')
    {
        pt4[j]=str[i];
        i++;
        j++;
    }
    printf(pt1);
    printf(pt2);
    printf(pt3);
    printf(pt4);
}
int main(){
    char str[LUNG];
    printf("Inserisci un indirizzo ip:\t");
    fgets(str,LUNG,stdin);
    controlla(str);

    return 0;
}
Barmar
  1. You should check each character that you're adding to ptX to see if it's a digit.
  2. You need to check the length of each substring. You can do this by checking the value of j after each loop.
  3. You have to reset j = 0 before each while loop.
  4. After each loop, make sure that the current character is a ., not \0. Otherwise you'll keep reading past the end of the string.
  5. In the last loop you should check for \n instead of ..
  6. The first argument to printf() should be a format string, not the string you want to print.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define LUNG 30
#define INTLUNG 30

void controlla(char *str){
    char pt1[INTLUNG],pt2[INTLUNG],pt3[INTLUNG],pt4[INTLUNG];
    int i=0,j=0;
    while(str[i] !='.' && str[i] != '\0')
    {
        if (!isdigit(str[i])) {
            printf("Not a valid address\n");
            return;
        }
        pt1[j]=str[i];
        i++;
        j++;
    }
    if (i == 0 || str[i] != '.' || j == 0 || j > 3) {
        printf("Not a valid address\n");
        return;
    }
    i++;
    pt1[j] = '\0';
    j = 0;
    while(str[i] !='.' && str[i] != '\0')
    {
        if (!isdigit(str[i])) {
            printf("Not a valid address\n");
            return;
        }
        pt2[j]=str[i];
        i++;
        j++;
    }
    if (str[i] != '.' || j == 0 || j > 3) {
        printf("Not a valid address\n");
        return;
    }
    i++;
    pt2[j] = '\0';
    j=0;
    while(str[i] !='.' && str[i] != '\0')
    {
        if (!isdigit(str[i])) {
            printf("Not a valid address\n");
            return;
        }
        pt3[j]=str[i];
        i++;
        j++;
    }
    if (str[i] != '.' || j == 0 || j > 3) {
        printf("Not a valid address\n");
        return;
    }
    i++;
    pt3[j] = '\0';
    j=0;
    while(str[i] != '\n' && str[i] != '\0')
    {
        if (!isdigit(str[i])) {
            printf("Not a valid address\n");
            return;
        }
        pt4[j]=str[i];
        i++;
        j++;
    }
    pt4[j] = '\0';
    if (j == 0 || j > 3) {
        printf("Not a valid address\n");
        return;
    }
    printf("%s %s %s %s\n", pt1, pt2, pt3, pt4);
}
int main(){
    char str[LUNG];
    printf("Inserisci un indirizzo ip:\t");
    fgets(str,LUNG,stdin);
    controlla(str);

    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How can I get a list of PHP errors occurred in a page?

来自分类Dev

How can I make Linux system calls from a C/C++ application, without using assembly, and in a cpu-independent manner?

来自分类Dev

How can I duplicate files and folders without content?

来自分类Dev

How can I return a value to a superclass without overriding the parent function?

来自分类Dev

How can I reinstall Windows 7 on a laptop without the drivers CD?

来自分类Dev

How can I get the application path in C?

来自分类Dev

Can I write the method name without using a String to pass the name of the method? (c#)

来自分类Dev

How to properly use monadic expressions in Haskell without getting parse errors?

来自分类Dev

How to do "SUDO UPDATE and SUDO UPGRADE" without errors (NEWBIE)

来自分类Dev

How can i peek next token in flex without removing it from buffer

来自分类Dev

Mercurial: How can a I make a snapshot of my working directory without doing a changeset?

来自分类Dev

How can i run cmd as administrator privileges by cmd command without using any shortcut of cmd?

来自分类Dev

How can I click space to pause the video without down the page in JavaScript

来自分类Dev

Can someone explain how I got a Windows EFI error message without trying to install Windows?

来自分类Dev

Can someone explain how I got a Windows EFI error message without trying to install Windows?

来自分类Dev

How can I embed C# assembly resources in the same assembly?

来自分类Dev

Git: I've a feature branch with 80+ commits. How can I safely merge it into develop/production without ruining the history?

来自分类Dev

In C#, how can I keep all my items in a listbox from changing to the color I set last?

来自分类Dev

How can I dual boot Ubuntu and Windows 10 but have my PC boot into windows without prompting me at start up

来自分类Dev

How can a custom receiver use the "ramp" namespace or can I modify the sample apps to use a custom namespace without re-writing support for RAMP?

来自分类Dev

How to emit JSON errors from JSON handlers without redefining `errorHandler` for the whole app?

来自分类Dev

Can I be informed that an activity intent was sent without registering an activity for it to start

来自分类Dev

How can I cancel an ngEvent?

来自分类Dev

How can I return a function?

来自分类Dev

How should I persist complex objects as strings in javascript

来自分类Dev

How can I define an UUID for a class, and use __uuidof, in the same way for g++ and Visual C++?

来自分类Dev

C++ Shared pointers. How can I change the underlying object's pointer for all copies?

来自分类Dev

How can I create simple interactive GUI for c++ OpenCV application?

来自分类Dev

How can I programatically determine where my C++ runtime libraries are?

Related 相关文章

  1. 1

    How can I get a list of PHP errors occurred in a page?

  2. 2

    How can I make Linux system calls from a C/C++ application, without using assembly, and in a cpu-independent manner?

  3. 3

    How can I duplicate files and folders without content?

  4. 4

    How can I return a value to a superclass without overriding the parent function?

  5. 5

    How can I reinstall Windows 7 on a laptop without the drivers CD?

  6. 6

    How can I get the application path in C?

  7. 7

    Can I write the method name without using a String to pass the name of the method? (c#)

  8. 8

    How to properly use monadic expressions in Haskell without getting parse errors?

  9. 9

    How to do "SUDO UPDATE and SUDO UPGRADE" without errors (NEWBIE)

  10. 10

    How can i peek next token in flex without removing it from buffer

  11. 11

    Mercurial: How can a I make a snapshot of my working directory without doing a changeset?

  12. 12

    How can i run cmd as administrator privileges by cmd command without using any shortcut of cmd?

  13. 13

    How can I click space to pause the video without down the page in JavaScript

  14. 14

    Can someone explain how I got a Windows EFI error message without trying to install Windows?

  15. 15

    Can someone explain how I got a Windows EFI error message without trying to install Windows?

  16. 16

    How can I embed C# assembly resources in the same assembly?

  17. 17

    Git: I've a feature branch with 80+ commits. How can I safely merge it into develop/production without ruining the history?

  18. 18

    In C#, how can I keep all my items in a listbox from changing to the color I set last?

  19. 19

    How can I dual boot Ubuntu and Windows 10 but have my PC boot into windows without prompting me at start up

  20. 20

    How can a custom receiver use the "ramp" namespace or can I modify the sample apps to use a custom namespace without re-writing support for RAMP?

  21. 21

    How to emit JSON errors from JSON handlers without redefining `errorHandler` for the whole app?

  22. 22

    Can I be informed that an activity intent was sent without registering an activity for it to start

  23. 23

    How can I cancel an ngEvent?

  24. 24

    How can I return a function?

  25. 25

    How should I persist complex objects as strings in javascript

  26. 26

    How can I define an UUID for a class, and use __uuidof, in the same way for g++ and Visual C++?

  27. 27

    C++ Shared pointers. How can I change the underlying object's pointer for all copies?

  28. 28

    How can I create simple interactive GUI for c++ OpenCV application?

  29. 29

    How can I programatically determine where my C++ runtime libraries are?

热门标签

归档