找单词编程题
#include <stdio.h>
#include <ctype.h>
char *fun(char *p)
{
int i=0,maxlenwordlen=0,wordbegin=-1,maxlenwordbegin=-1;
if(p)
{
for(;*(p+i)!='\0';)
{
while(isspace(*(p+i)))
{
i++;
}
wordbegin=i;
while(*(p+i)!='\0' && !isspace(*(p+i)))
{
i++;
}
if(i-wordbegin>maxlenwordlen)
{
maxlenwordbegin=wordbegin;
maxlenwordlen=i-wordbegin;
}
}
}
if(maxlenwordlen)
{
*(p+maxlenwordbegin+maxlenwordlen)='\0';
printf("最长单词是:");
return (p+maxlenwordbegin);
}
else
{
printf("你的输入不含有任何单词!\r\n");
return p;
}
}
int main()
{
char TBuf[1024],*pword;
gets(TBuf);
pword=fun(TBuf);
while(*pword!='\0'&& *pword!=' ')
putchar(*pword++);
return 0;
}
2. 猜单词 程序设计题
额 别告诉我你是南邮的 我这有个以前做的的 只是“猜单词”这一个模块的程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define max 1024
#define min 50
void main()
{
FILE *fp;
char c;
int i=0;
int j=0;
int x=0;
char z[]={0};
char str[max][min]={0};
if((fp=fopen("单词库.txt","r"))==NULL)
{
printf("can not open file word.txt\n");
exit(0);
}
c=fgetc(fp);
while(c!=EOF)
{
if(c!=' ')
{
str[i][j]=c;
j++;
}
else
{
i++;
j=0;
}
c=fgetc(fp);
}
//产生随机数,0-i
srand((unsigned int)time(NULL));
int n=rand()/20;
while((n<0)||(n>i))
{
n=rand()%9;
}
x=strlen(str[n]);
printf("#######guess begin######\n");
printf("\n");
for(int g=0;g<x;g++)
strcat(z,"_");
for(int u=0;u<x;u++)printf("_ ");
char gs;
while(strcmp(z,str[n])!=0)
{ printf("请输入字母\n");
scanf("%c",&gs);
getchar();
int c=0;c++;
int flag=0;
if(c>=4*x)break;
for(int i=0;str[n][i]!='\0';i++)
{
if(str[n][i]==gs)
{ z[i]=str[n][i];
printf(z,"\n");
flag=1;
break;
}
}
if(!flag)
{
printf(z,"字母%不在单词中\n",gs);
}
}
if(strcmp(z,str[n])==0)
printf("恭喜你猜对了%s",z);
fclose(fp);
}
3. C编程题:提取字符串中的单词
csdn已为您找到关于java提取字符串中的单词相关内容,包含java提取字符串中的单词相关文档代码介绍、相关教程视频课程
4. c语言编程题 折半法找单词
查找基本算法:
折半算法:
intSearch_Bin(intdata[],intkey,intlength)
{ // 在数组data中折半查找其值等于key的数据元素
int low,high,mid;
low=0; // 置区间初值
high=length-1;
while(low<=high){
mid=(low+high)/2;
if (key==data[mid]) // 找到待查元素
return mid;
else
if (key<data[mid])high=mid-1; // 继续在前半区间进行查找
else
low=mid+1; // 继续在后半区间进行查找
}
return -1;// 顺序表中不存在待查元素
}
5. 编程题:给定字符串s,其内容为英语长句,其中包含英语单词,标点符号,空格等内容,每个英语单词使用标
两个思路:
逐个查找标点或者空格(连续分隔符算一个),然后分类存储;
直接用string.h(C语言)中的strchr和substr(这个需要自定义)来划分单词。
祝你成功。
6. 《编程题》单词接龙求教java高人解答
先将单词复制一份。拿你的例子说,5个单词,由于每个可以出现两次,那么最终就是10个单词。
构造图结构,你把每个单词当成一个节点,遍历所有节点,能收尾相连且不包含节点的就连一根线,做成一个有向图。
遍历整个图,找到最长的
由于你最多只有20个单词,这种方法是绝对可行的,相当于把所有可能的龙找出来并挑选最长的。
算我无聊,写了份代码,你参考下吧,输入输出我没怎么管哈
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class FindLongestDragon
{
public static void main(String args[])
{
getLongestDragon('a', "at", "touch", "cheat", "choose", "tact");
}
public static void getLongestDragon(char startChar, String ... nodesStr)
{
List<Node> nodes = new ArrayList<Node>();
for (String nodeStr : nodesStr)
{
nodes.add(new Node(nodeStr));
nodes.add(new Node(nodeStr));
}
//遍历所有节点关系,构成图
for (Node node1 : nodes)
{
for (Node node2: nodes)
{
if (node1 != node2)
{
checkIsLink(node1, node2);
}
}
}
//遍历
for (Node node : getStartWithCharNodes(nodes, startChar))
{
Dragon dragon = new Dragon();
dragon.append(node);
dragon.findNextNode();
}
//输出所有的龙
Collections.sort(Dragon.dragons);
for (Dragon dragon : Dragon.dragons)
{
System.out.println(dragon);
}
}
//b是否能和a相连
public static void checkIsLink(Node a, Node b)
{
String nameA = a.getName();
String nameB = b.getName();
//存在包含关系
if (nameA.endsWith(nameB) || nameB.startsWith(nameA))
{
return;
}
if (getLinkStr(a, b) != null)
{
a.addChild(b);
}
}
public static String getLinkStr(Node a, Node b)
{
String nameA = a.getName();
String nameB = b.getName();
//从第二个字符开始检查是否相连
for (int i = 1, length = nameA.length(); i < length; i++)
{
String linkStr = nameA.substring(i);
if (nameB.startsWith(linkStr))
{
return linkStr;
}
}
return null;
}
private static List<Node> getStartWithCharNodes(List<Node> list, char startChar)
{
List<Node> nodes = new ArrayList<Node>();
int size = list.size();
for (int i = 0; i < size; i+=2)
{
Node node = list.get(i);
if (node.getName().charAt(0) == startChar)
{
nodes.add(node);
}
}
return nodes;
}
}
class Dragon implements Comparable<Dragon>
{
private List<Node> nodes = new ArrayList<Node>();
public static List<Dragon> dragons = new ArrayList<Dragon>();
public void findNextNode()
{
Node lastNode = nodes.get(nodes.size() - 1);
boolean hasNextNode = false;
for (Node nextNode : lastNode.getChildren())
{
if (!nodes.contains(nextNode))
{
hasNextNode = true;
append(nextNode);
findNextNode();
removeTail();
}
}
//找到了尽头
if (!hasNextNode)
{
Dragon = this.();
if (!dragons.contains())
{
dragons.add();
}
}
}
private Dragon ()
{
Dragon dragon = new Dragon();
for (Node node : this.nodes)
dragon.append(node);
return dragon;
}
public void append(Node node)
{
nodes.add(node);
}
public void removeTail()
{
nodes.remove(nodes.size() - 1);
}
public String toString()
{
StringBuilder sb = new StringBuilder();
//展示所有的节点
String allNodeLinkStr = getAllNodeLinkStr();
sb.append(allNodeLinkStr).append(" ");
sb.append(allNodeLinkStr.length()).append(" ");
for (Node node : nodes)
{
sb.append(node.getName()).append(" ");
}
return sb.toString();
}
@Override
public int compareTo(Dragon o)
{
return o.getLength() - this.getLength();
}
public int getLength()
{
return getAllNodeLinkStr().length();
}
public String getAllNodeLinkStr()
{
StringBuilder sb = new StringBuilder();
//展示所有的节点
sb.append(nodes.get(0));
for (int i = 1, length = nodes.size(); i < length; i++)
{
Node node = nodes.get(i);
sb.append(node.getName().substring(FindLongestDragon.getLinkStr(nodes.get(i - 1), node).length()));
}
return sb.toString();
}
public boolean equals(Object o)
{
if (o instanceof Dragon)
{
Dragon d = (Dragon)o;
if (d.nodes.size() == this.nodes.size())
{
int length = this.nodes.size();
for (int i = 0; i < length; i++)
{
if (!d.nodes.get(i).getName().equals(this.nodes.get(i).getName()))
{
return false;
}
}
return true;
}
}
return false;
}
}
class Node
{
private List<Node> children = new ArrayList<Node>();
private String name;
public Node(String name)
{
super();
this.name = name;
}
public List<Node> getChildren()
{
return children;
}
public String getName()
{
return name;
}
public void addChild(Node e)
{
this.children.add(e);
}
public String toString()
{
return this.getName();
}
}
7. 一道c语言编程题,寻找字符串最长的单词并输出
这一行写错了,p1前要加个*号:
printf("%c",p1++);
要改成
printf("%c", *p1++);
看别人写的程序很难懂,不如自己重写一下,已经测试通过:
#include <stdio.h>
#include <string.h>
int main()
{
char s[128];
char *p1, *p2;
int max=0, len=0;
printf("Input a string: ");
gets(s);
p1=s;
for (int i=0; i<strlen(s); i++)
{
if (s[i]==' ') // 如果当前字符为空格,则比较当前单词长度是否大于最大值,再将长度复位。
{
if (len>max)
{
max=len;
p2=p1;
}
len=0;
} else // 如果当前字符非空,如果当前长度为0,则表示新单词。
{
if (len==0)
p1=&s[i];
++len;
}
}
while (*p2 && *p2!=' ')
printf("%c", *p2++);
}
8. VC++找单词问题
#include <iostream>
#include <string.h>
using namespace std;void findshort(char *f1, char *f2)
{
int length = strlen(f1);
int flag = 0;
int short_len = 100;
int len = 0;
char *p = f1;
char *short_add;
int i; cout<<f1<<endl;
for(i = 0; i <= length; i++)
{
if(flag == 0 && f1[i] != ' ')
{
flag = 1;
len++;
p = &f1[i];
}
else if(flag == 1 && f1[i] != ' ' && f1[i] != '\0')
{
len++;
}
else if(flag == 1 && f1[i] == ' ' || f1[i] == '\0')
{
if(len < short_len)
{
short_add = p;
short_len = len;
}
len = 0;
flag = 0;
}
}
strncpy(f2, short_add, short_len);
f2[short_len] = '\0';
}int main()
{
char f1[100];
char f2[100];
cout<<"输入一行字符串:"<<endl;
gets(f1);
findshort(f1, f2);
cout<<"最短单词:"<<f2<<endl; return 0;
}
9. Java 由用户输入一个单词,编程在一个文本文件中查找这个单词
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.UnsupportedEncodingException;
importjava.util.Scanner;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassDay11_A{
privatestaticScannersc=null;
publicstaticvoidmain(String[]args){
sc=newScanner(System.in);
Filefile=newFile("K:/Test/TestRead.txt");
Stringstr=null,src=null;
if(file.canExecute()){
str=readFile(file);
}else{
System.out.println("文件不存在!");
return;
}
while(true){
System.out.println("请输入想查找的单词:over为结束查找!");
src=sc.nextLine();
if(src.equalsIgnoreCase("over"))
break;
System.out.println("查找结果:"+look(src,str));
}
}
privatestaticStringreadFile(Filefile){
BufferedReaderbr=null;
StringBuilderstu=newStringBuilder();
try{
br=newBufferedReader(newInputStreamReader(newFileInputStream(file),"GBK"));
for(Stringstr=br.readLine();str!=null;str=br.readLine()){
stu.append(str);
stu.append(System.lineSeparator());
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(br!=null){
try{
br.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
returnstu.toString();
}
privatestaticintlook(Stringregex,Stringstr){
Matchermat=Pattern.compile(regex).matcher(str);
intcount=0;
while(mat.find()){
count++;
}
returncount;
}
}