本文介绍了重新启动while循环C时不整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让这个code的工作,但我不知道如何重新启动内部while循环。我会怎么做呢?

  / *
 *返回一个指向任何字符第一次出现上述< STOP>
 *在给定的<串GT;或NULL,如果在<串GT;未包含字符
 *上述<停止取代。
 *****
 *您可以*不*使用整数或数组索引。
 *****
 * /
字符* find_any_ptr(字符*字符串,字符*停止){    字符* newstring =(字符*)0;    而(*停!='\\ 0'){
            而(*串!='\\ 0'){
                    如果(*字符串== *停止){
                            如果(newstring<!字符串|| newstring =(的char *)0){
                                    串++;
                            }其他{
                                    newstring =串;
                                    串++;
                            }
                    }
            }
            停止++;
    }
    返回newstring; //占位符
}


解决方案

这是比较简单的使用只是一个字符指针字符串和一个指向停止。对于你的字符串每一个字符,你在停止,中返回字符字符串的匹配,或 NULL 如果没有找到匹配的:

 的#include<&stdio.h中GT;字符* find_any_index(字符字符串[],CHAR停止[]){    的char * p =串;
    字符* SP = NULL;    而(* p)的
    {
        SP =停止;
        而(* SP)
        {
            如果(*藻== * p)的
                回磷;
            SP ++;
        }
        p ++;
    }    返回NULL;
}INT主(INT ARGC,字符** argv的){    如果(argc个3;){
        输出(用法:%s字符串索引字表\\ n,argv的[0]);
    }    的printf(\\ n字符串:%S \\ n停止:%S \\ n \\ n,ARGV [1],ARGV [2]);
    的printf(字符串第一个字符匹配停止一个字符:%S \\ n \\ n,find_any_index(ARGV [1],ARGV [2]));    返回0;
}

输出

  $ ./bin/find_substr_str thisIsAstring万世 字符串:thisIsAstring
 停车:万世 字符串第一个字符匹配停止一个char:sIsAstring

I'm trying to get this code to work, but I have no idea how to restart the inner while loop. How would I do it?

/*
 * Return a pointer to the first occurrence of any character in <stop>
 * in the given <string> or NULL if the <string> contains no characters
 * in <stop>.
 *****
 * YOU MAY *NOT* USE INTEGERS OR ARRAY INDEXING.
 *****
 */
char *find_any_ptr(char *string, char* stop) {

    char *newstring = (char*)0;

    while(*stop != '\0'){
            while(*string != '\0') {
                    if(*string == *stop){
                            if(newstring < string || newstring != (char*)0){
                                    string++;
                            }else{
                                    newstring = string;
                                    string++;
                            }
                    }
            }
            stop++;
    }
    return newstring;   // placeholder
}
解决方案

This is relatively simple using nothing but a character pointer to the string and a pointer to stop. For each character in your string, you compare against each character in stop, returning the character in string on match, or NULL if no match is found:

#include <stdio.h>

char *find_any_index(char string[], char stop[]) {

    char *p = string;
    char *sp = NULL;

    while (*p)
    {
        sp = stop;
        while (*sp)
        {
            if (*sp == *p)
                return p;
            sp++;
        }
        p++;
    }

    return NULL;
}

int main (int argc, char **argv) {

    if (argc < 3) {
        printf ("usage:  %s string stoplist\n", argv[0]);
    }

    printf ("\n string: %s\n stop  : %s\n\n", argv[1], argv[2]);
    printf (" first char in string matching a char in stop: %s\n\n", find_any_index (argv[1], argv[2]));

    return 0;
}

Output

$ ./bin/find_substr_str thisIsAstring mase

 string: thisIsAstring
 stop  : mase

 first char in string matching a char in stop: sIsAstring

这篇关于重新启动while循环C时不整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:52
查看更多