使用指针算法编码字符串长度函数

时间:2018-11-17 23:03:15

标签: c string pointers string-length

我正在学习CS的初学者CS课上,我们的任务是编写一个函数,仅使用字符串指针查找字符串长度。我有以下代码:

import React from 'react'
import {
  Modal,
  TouchableWithoutFeedback,
  Text,
  StyleSheet,
  Platform,
  View,
  Picker,
  TextInput,
  TouchableOpacity
} from 'react-native';

class DropDown extends React.Component {
  constructor(props) {
  super(props);
  this.state = {
    modalVisible: false,
  };
}
render() {
  if (Platform.OS === 'android') {
    return (
    <Picker
      selectedValue={this.props.value}
      onValueChange={this.props.onValueChange}>
      {this.props.items.map((i, index) => (
        <Picker.Item key={index} label={i.label} value={i.value} />
      ))}
    </Picker>
  );
} else {
  const selectedItem = this.props.items.find(
    i => i.value === this.props.value
  );
  const selectedLabel = selectedItem ? selectedItem.label : '';
  return (
    <View style={styles.inputContainer}>
      <TouchableOpacity
        onPress={() => this.setState({ modalVisible: true })}>

        <TextInput
          pointerEvents="none"
          style={styles.input}
          editable={false}
          placeholder="Select language"
          onChangeText={searchString => {
            this.setState({ searchString });
          }}
          value={selectedLabel}
        />
      </TouchableOpacity>
      <Modal
        animationType="slide"
        transparent={true}
        visible={this.state.modalVisible}>
        <TouchableWithoutFeedback
          onPress={() => this.setState({ modalVisible: false })}>
          <View style={styles.modalContainer}>
            <View style={styles.buttonContainer}>
              <Text
                style={{ color: 'blue' }}
                onPress={() => this.setState({ modalVisible: false })}>
                Done
              </Text>
            </View>
            <View>
              <Picker
                selectedValue={this.props.value}
                onValueChange={this.props.onValueChange}>
                {this.props.items.map((i, index) => (
                  <Picker.Item
                    key={index}
                    label={i.label}
                    value={i.value}
                  />
                ))}
              </Picker>
            </View>
          </View>
        </TouchableWithoutFeedback>
      </Modal>
    </View>
  );
}
}
}

const styles = StyleSheet.create({
  container: {
  flex: 1,
  justifyContent: 'center',
  alignItems: 'center',
},
content: {
  marginLeft: 15,
  marginRight: 15,
  marginBottom: 5,
  alignSelf: 'stretch',
  justifyContent: 'center',
},
inputContainer: {
  borderBottomColor: 'gray',
  borderBottomWidth: 1
},
input: {
  height: 40,
  paddingLeft: 8
},
modalContainer: {
  position: 'absolute',
  bottom: 0,
  width: '100%',
  backgroundColor: '#fff',
},
buttonContainer: {
  justifyContent: 'flex-end',
  flexDirection: 'row',
  padding: 4,
  backgroundColor: '#fff',
},
});

export default DropDown

我不确定在哪里出现细分错误。我尝试在strlength函数中创建第二个指针,该指针等于str并对其进行递增,但这也给我带来了分段错误。任何见识将不胜感激!

1 个答案:

答案 0 :(得分:1)

data Type t where
  VNat :: Type Nat
  VString :: Type String
  VArrow :: Type a -> Type b -> Type (a -> b)
  VPair :: Type a -> Type b -> Type (a, b) 
  VUnit :: Type ()
data SomeType = forall t. SomeType (Type t)
data SomeHom = forall a b. SomeHom (Type a) (Type b) (THom a b)

type Context = [(TVar, SomeType)] 
getType :: Context -> SomeType
getType [] = SomeType VUnit 
getType ((_, SomeType ttyp) :: gamma) =  
   case getType gamma of
       SomeType ctxT -> SomeType (VPair ttyp
find :: TVar -> Context -> SomeHom 
find tvar ((tvar’, ttyp) :: gamma)
   | tvar == tvar’ =
       case (ttyp, getType gamma) of
         (SomeType t, SomeType ctxT) ->
          SomeHom (VPair t ctxT) t Fst

您应该在scanf之前为char *str; int comp; printf("Please enter the string: "); scanf("%s", str); 在堆中分配内存(使用malloc)。如果您不想使用malloc,请将其更改为char [number],以便它可以在堆栈而不是堆中分配内存