[Summary] algorithm notes strings stored in the two-dimensional array

#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;

//字符串存到二维数组里面(①存到一维数组  ②一维数组转存到二维数组) 
int main()
{
  char str[100];
  while(gets(str)){
	cout<<str<<endl;
    int  len = strlen(str), r=0,h=0,i;
    char ans[100][100]; 

	//再存到一个二维数组里面
    for(i=0; i<len; i++){
        if(str[i] != ' ')
            ans[r][h++] = str[i];
        else{
            ans[r++][h] = '\0';
            h=0;//h重新置0
        }
    }


    //输入二维数组
    for(i=0; i<=r; i++){
        printf("%s",ans[i]);
    }
    cout<<endl;
	
    //清空初始化数组,以下两种方式都可以
    //memset(ans, 0 , sizeof(ans));
	memset(ans, '\0', sizeof(ans)); 
  }

  return 0;
}

 

Published 63 original articles · won praise 13 · views 40000 +

Guess you like

Origin blog.csdn.net/changreal/article/details/88241779